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.