Java Reference
In-Depth Information
The program registers action listeners for check boxes, radio buttons, and the text field
(lines 76-116).
When a check box is checked or unchecked, the listener's actionPerformed method is
invoked to process the event (lines 79, 85). The current font name and size used in JLabel are
obtained from jlblMessage.getFont() using the getName() and getSize() methods
(line 128). The font styles ( Font.BOLD and Font.ITALIC ) are specified in the check boxes.
If no font style is selected, the default font style is Font.PLAIN (line 121). The font style is an
integer 0 for Font.PLAIN , 1 for Font.BOLD , and 2 for Font.ITALIC . The font style can be
combined by adding together the integers that represent the fonts (lines 122-123). For example,
Font.BOLD + Font.ITALIC is 3 , which represents a combined font of bold and italic.
The setFont method (line 127) defined in the Component class is inherited in the
JLabel class. This method automatically invokes the repaint method. Invoking setFont
on jlblMessage automatically repaints jlblMessage .
A check box fires an ItemEvent and then an ActionEvent when it is clicked. You could
process either the ItemEvent or the ActionEvent to redisplay the message. The program in
this example processes the ActionEvent . If you want to process the ItemEvent , create a
listener for ItemEvent and register it with a check box. The listener must implement the
itemStateChanged handler to process an ItemEvent . For example, the following code
registers an ItemListener with jchkBold :
register listeners
check boxes
// To listen for ItemEvent
jchkBold.addItemListener( new ItemListener() {
@Override /** Handle ItemEvent */
public void itemStateChanged(ItemEvent e)
{
setNewFont();
}
});
When a radio button is clicked, its action event listener sets the corresponding foreground
color in jlblMessage (lines 93, 99, 105).
The program creates a ButtonGroup and puts three JRadioButton instances ( jrbRed ,
jrbGreen , and jrbBlue ) in the group (lines 50-53) so they can only be selected exclusively—
the text will be either red or green or blue.
A radio button fires an ItemEvent and then an ActionEvent when it is selected or
deselected. You could process either the ItemEvent or the ActionEvent to choose a color.
This program processes the ActionEvent . As an exercise, rewrite the code using the
ItemEvent .
After you type a new message in the text field and press the Enter key, a new message is
displayed. Pressing the Enter key on the text field triggers an action event. The listener sets a
new message in jlblMessage (line 113).
The requestFocusInWindow() method (line 114) defined in the Component class requests
the component to receive input focus. Thus, jtfMessage.requestFocusInWindow()
requests the input focus on jtfMessage . You will see the cursor on jtfMessage after the
actionPerformed method is invoked.
The pack() method (line 23) automatically sizes the frame according to the size of the
components placed in it.
radio buttons
radio button group
ActionEvent for
JTextField
requestFocusInWindow()
pack()
17.1
Can a JButton , JLabel , JCheckBox , JRadioButton , and JTextField fire an
ActionEvent ?
Check
Point
17.2
Can a JButton , JLabel , JCheckBox , JRadioButton , and JTextField fire an
ItemEvent ?
17.3
What happens after invoking jtfMessage.requestFocusInWindow() ?
 
Search WWH ::




Custom Search