Chapter 17 - GUI Components Flashcards

0
Q

How do you group radio buttons together?

A

Use the ButtonGroup class:
ButtonGroup group = new ButtonGroup();
group.add(jrbName1);
group.add(jrbName2);

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
1
Q

What events are fired from JCheckBox and JRadioButton?

A

When clicked, first an ItemEvent is fired, then an ActionEvent

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

How do you set mnemonic keys for check boxes and the likes?

A

By using the setMnemonic method provided by these classes.
jchkName.setMnemonic(‘N’);
Notice that the method takes a character as input (not a string!)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

When does a JTextField usually fire an action event?

A

After the “enter” key on the keyboard is pressed (if the text field is in focus, that is).

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

What component do you use if you need to let the user enter multiple lines of text?

A

You use the class JTextArea

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

JTextField and JTextArea inherits JTextComponent. What are the methods inherited from JTextComponent?

A

getText, setText, isEditable and setEditable

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

JTextArea does not handle scrolling. How do you add scrolling to s JTextArea?

A

You use the class JScrollPane.
Let’s say you have a JTextArea named jtaName. You add scrolling like this:
JScrollPane scrollPane = new JScrollPane(jtaName);
The object scrollPane is now a text area with scrolling.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

What components can you put in a JScrollPane? What does JScrollPane do?

A

You can add any swing GUI components. JScrollPane adds horizontal and vertical scrolling if the component is too large to fit.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

What is a JComboBox? What is it good for?

A

Also known as a choice list or drop-down menu.

It’s useful for limiting the user’s range of choice, and avoids the cumbersome validation of data input.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

How do you code a JComboBox with 4 generic items, blue foreground color, white background color, and item 3 selected as default?

A

JComboBox jcb = new JComboBox(new Object[] {“item 1”, “item 2”, “item 3”, “item 4”});

jcb. setForeground(Color.blue);
jcb. setBackground(Color.white);
jcb. setSelectedItem(“item 3”);

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

When does a JComboBox fire an ActionEvent?

A

Whenever an item is selected

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

When does a JComboBox fire an ItemEvent?

A

When a new item is selected JComboBox fires ItemEvent twice. Once for deselecting the previously selected item, and once for selecting the new item.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

How is a JList different from a JComboBox?

A

A JList is basically the same as a JComboBox, but it has 3 selection modes:

  1. Single selection; the same as JComboBox, only one item can be selected at a time.
  2. Single interval selection; can select multiple items at once as long as they’re in one contigous interval
  3. Multiple interval selection; can select multiple items in multiple intervals.
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

What method must item listeners implement?

A

They must implement itemStateChanged(ItemEvent e) to respond to item events.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

What event does a JList fire, and what method must the listener of the corresponding interface implement to process the event?

A

JList fires a ListSelectionEvent. The ListSelectionListener must implement the method valueChanged(ListSelectionEvent e) to process the event.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

What is the difference between a JScrollPane and a JScrollBar?

A

A JScrollPane is a container that handles scrolling for components inside the container. A JScrollBar is the component used in JScrollPane (and anywhere else) that facilitates scrolling

16
Q

There are 3 ways a user can change the value of a scroll bar by using the mouse. What are they?

A

The user can:

  1. click on the unit increment/decrement buttons at the ends of the scroll bar.
  2. Drag the “bubble” back and forth.
  3. Click on the space between the bubble and the unit incr/decr buttons to “block incr/decr”.
17
Q

When the value of a JScrollBar is changed, what event is fired?

A

An AdjustmentEvent

18
Q

What handler method must an AdjustmentListener implement?

A

The adjustmentValueChanged handler

19
Q

What is the difference between JSlider and JScrollBar?

A

They are similar, but JSlider has more properties and can appear in many forms. JScrollBar is typically used for scrolling, while JSlider is used for selecting between a range of values.

20
Q

What are the properties / data fields of javax.swing.JSlider, and what do they represent?

A

maximum (int): max value represented by slider (default = 100).

minimum (int): min value represented by slider (default = 0).

value (int): current value represented by slider.

orientation (int): orientation of slider (default = JSlider.HORIZONTAL).

paintLabels (boolean): true if labels are painted at tick marks (default = false).

paintTicks (boolean): true if ticks are painted on the slider (default = false).

paintTrack (boolean): true if track is painted on the slider (default = true).

majorTickSpacing (int): number of pixels between major ticks (default = 0).

minorTickSpacing (int): number of pixels between minor ticks (default = 0).

inverted (boolean): true to reverse the value range (default = false).

21
Q

When the value of a JSlider is changed, when event is fired by the slider?

A

ChangeEvent

javax.swing.event.ChangeEvent

23
Q

What method must a ChangeListener implement to handle a ChangeEvent?

A

It must implement stateChanged.

stateChanged(ChangeEvent e).

24
Q
Clicking a JCheckBox object generates \_\_\_ events.
A. ActionEvent
B. ItemEvent
C. ComponentEvent
D. ContainerEvent
A

A and B are correct

25
Q
The method \_\_\_\_ adds a text area jta to a scrollpane jScrollPane.
A. jScrollPane.add(jta)
B. jScrollPane.insert(jta)
C. jScrollPane.addItem(jta)
D. None of the above.
A

A is correct

26
Q
How many items can be added into a JComboBox object?
A. 0
B. 1
C. 2
D. Unlimited
A

The correct answer is D. Unlimited

27
Q
\_\_\_ returns the selected item on a JComboBox jcbo.
A. jcbo.getSelectedIndex()
B. jcbo.getSelectedItem()
C. jcbo.getSelectedIndices()
D. jcbo.getSelectedItems()
A

The correct answer is

B. jcbo.getSelectedItem()

28
Q
The method \_\_\_ adds an item s into a JComboBox jcbo.
A. jcbo.add(s)
B. jcbo.addChoice(s)
C. jcbo.addItem(s)
D. jcbo.addObject(s).
A

The correct answer is

C. jcbo.addItem(s)

29
Q
Clicking a JComboBox object always generates \_\_\_ events.
A. ActionEvent
B. ItemEvent
C. MouseEvent 
D. KeyEvent
E. WindowEvent
A

The correct answer is A and C.
Explanation: It generates ActionEvent and MouseEvent. Note that any GUI component fires a MouseEvent when mouse is clicked on the component. No ItemEvent is generated if the current item is re-selected.

30
Q

Clicking a JComboBox object generates an ItemEvent event,
A. if an item is selected.
B. if a new item is selected.

A

B. if a new item is selected.

31
Q
\_\_\_\_ is a component that enables the user to choose a single value or multiple values.
A. A text field
B. A combo box
C. A list
D. A label
A

The correct answer is

C. A list

32
Q
Clicking a JList object generates \_\_\_\_ events.
A. ActionEvent and ItemEvent
B. ItemEvent and ComponentEvent
C. ComponentEvent and ContainerEvent
D. ActionEvent and ContainerEvent
A

A. ActionEvent and ItemEvent

33
Q

Which of the following statements are true?
A. Every Swing GUI component has a default constructor.
B. You can create a scroll bar by specifying its orientation.
C. JScrollBar fires an ActionEvent.
D. JScrollBar fires an AdjustmentEvent.
E. A listener of a JScrollBar must implement AdjustmentEvent and the adjustmentValueChanged method.

A

A, B, D and E are true

34
Q

Which of the following statements are true?
A. You can create a JSlider bar by specifying its orientation.
B. JSlider fires an ActionEvent.
C. JSlider fires an AdjustmentEvent.
D. JSlider fires a javax.swing.event.ChangeEvent.
E. A listener of a JSlider must implement ChangeEvent and the stateChanged method.

A

All but C are true

35
Q
The following are properties of a JSlider.
A. minimum
B. maximum 
C. orientation
D. visibleAmount
A

All but D are correct

36
Q

Can you get the height and width of a window before the frame is created?

A

No!

37
Q

How can you get the height and width of a window after it is resized?

A

You can add a ComponentListener to the frame. For example, an instance of the following class can be added to a frame to change and repaint it every time it’s resized:

class WindowResizeListener extends ComponentAdapter {
@Override
public void componentResized(ComponentEvent e) {
messageY = (getHeight() / 6);
repaint();
}
}

38
Q

Describe in a few steps how to make a program show multiple windows / create subwindows.

A
  1. Create a class for the main window, and a class for the subwindow.
  2. Create an instance of the subwindow class in the main class.
  3. In the main class, create a JFrame to hold the subwindow panel (unless the subwindow itself is a JFrame).
  4. In the listener you want to create the subwindow, do operations you want performed by the subclass, and set the JFrame that holds the subwindow to visible.
  5. In the constructor of the main class, add the subwindow panel, and do preferred operations on the frame, like:
    frame. setTitle(“asdg”);
    frame. pack();
    frame. awdgfawertw