Chapter 17 - GUI Components Flashcards
How do you group radio buttons together?
Use the ButtonGroup class:
ButtonGroup group = new ButtonGroup();
group.add(jrbName1);
group.add(jrbName2);
What events are fired from JCheckBox and JRadioButton?
When clicked, first an ItemEvent is fired, then an ActionEvent
How do you set mnemonic keys for check boxes and the likes?
By using the setMnemonic method provided by these classes.
jchkName.setMnemonic(‘N’);
Notice that the method takes a character as input (not a string!)
When does a JTextField usually fire an action event?
After the “enter” key on the keyboard is pressed (if the text field is in focus, that is).
What component do you use if you need to let the user enter multiple lines of text?
You use the class JTextArea
JTextField and JTextArea inherits JTextComponent. What are the methods inherited from JTextComponent?
getText, setText, isEditable and setEditable
JTextArea does not handle scrolling. How do you add scrolling to s JTextArea?
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.
What components can you put in a JScrollPane? What does JScrollPane do?
You can add any swing GUI components. JScrollPane adds horizontal and vertical scrolling if the component is too large to fit.
What is a JComboBox? What is it good for?
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 do you code a JComboBox with 4 generic items, blue foreground color, white background color, and item 3 selected as default?
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”);
When does a JComboBox fire an ActionEvent?
Whenever an item is selected
When does a JComboBox fire an ItemEvent?
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 is a JList different from a JComboBox?
A JList is basically the same as a JComboBox, but it has 3 selection modes:
- Single selection; the same as JComboBox, only one item can be selected at a time.
- Single interval selection; can select multiple items at once as long as they’re in one contigous interval
- Multiple interval selection; can select multiple items in multiple intervals.
What method must item listeners implement?
They must implement itemStateChanged(ItemEvent e) to respond to item events.
What event does a JList fire, and what method must the listener of the corresponding interface implement to process the event?
JList fires a ListSelectionEvent. The ListSelectionListener must implement the method valueChanged(ListSelectionEvent e) to process the event.
What is the difference between a JScrollPane and a JScrollBar?
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
There are 3 ways a user can change the value of a scroll bar by using the mouse. What are they?
The user can:
- click on the unit increment/decrement buttons at the ends of the scroll bar.
- Drag the “bubble” back and forth.
- Click on the space between the bubble and the unit incr/decr buttons to “block incr/decr”.
When the value of a JScrollBar is changed, what event is fired?
An AdjustmentEvent
What handler method must an AdjustmentListener implement?
The adjustmentValueChanged handler
What is the difference between JSlider and JScrollBar?
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.
What are the properties / data fields of javax.swing.JSlider, and what do they represent?
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).
When the value of a JSlider is changed, when event is fired by the slider?
ChangeEvent
javax.swing.event.ChangeEvent
What method must a ChangeListener implement to handle a ChangeEvent?
It must implement stateChanged.
stateChanged(ChangeEvent e).
Clicking a JCheckBox object generates \_\_\_ events. A. ActionEvent B. ItemEvent C. ComponentEvent D. ContainerEvent
A and B are correct
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 is correct
How many items can be added into a JComboBox object? A. 0 B. 1 C. 2 D. Unlimited
The correct answer is D. Unlimited
\_\_\_ returns the selected item on a JComboBox jcbo. A. jcbo.getSelectedIndex() B. jcbo.getSelectedItem() C. jcbo.getSelectedIndices() D. jcbo.getSelectedItems()
The correct answer is
B. jcbo.getSelectedItem()
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).
The correct answer is
C. jcbo.addItem(s)
Clicking a JComboBox object always generates \_\_\_ events. A. ActionEvent B. ItemEvent C. MouseEvent D. KeyEvent E. WindowEvent
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.
Clicking a JComboBox object generates an ItemEvent event,
A. if an item is selected.
B. if a new item is selected.
B. if a new item is selected.
\_\_\_\_ 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
The correct answer is
C. A list
Clicking a JList object generates \_\_\_\_ events. A. ActionEvent and ItemEvent B. ItemEvent and ComponentEvent C. ComponentEvent and ContainerEvent D. ActionEvent and ContainerEvent
A. ActionEvent and ItemEvent
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, B, D and E are true
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.
All but C are true
The following are properties of a JSlider. A. minimum B. maximum C. orientation D. visibleAmount
All but D are correct
Can you get the height and width of a window before the frame is created?
No!
How can you get the height and width of a window after it is resized?
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();
}
}
Describe in a few steps how to make a program show multiple windows / create subwindows.
- Create a class for the main window, and a class for the subwindow.
- Create an instance of the subwindow class in the main class.
- In the main class, create a JFrame to hold the subwindow panel (unless the subwindow itself is a JFrame).
- 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.
- 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