670 Graphical User Interface Components: Part 1 Chapter (Cedant web hosting)
670 Graphical User Interface Components: Part 1 Chapter 12 91 // user clicked boldButton 92 else if ( event.getSource() == boldButton ) 93 field.setFont( boldFont ); 94 95 // user clicked italicButton 96 else if ( event.getSource() == italicButton ) 97 field.setFont( italicFont ); 98 99 // user clicked boldItalicButton 100 else if ( event.getSource() == boldItalicButton ) 101 field.setFont( boldItalicFont ); 102 } 103 104 } // end private inner class RadioButtonHandler 105 106 } // end class RadioButtonTest Fig. 12.12 Fig. 12.12 Creating and manipulating radio button (part 3 of 3). Lines 33 44 in the constructor define each JRadioButton object and add it to the application window s content pane. Each JRadioButtonis initialized with a constructor call like line 33. This constructor supplies the label that appears to the right of the JRadioButtonby default and the initial state of the JRadioButton. A truesecond argument indicates that the JRadioButtonshould appear selected when it is displayed. JRadioButtons, like JCheckBoxes, generate ItemEvents when they are clicked. Lines 47 51 create an instance of inner class RadioButtonHandler(defined at lines 82 104) and register it to handle the ItemEvent generated when the user clicks any one of the JRadioButtons. Line 54 instantiates a ButtonGroupobject and assigns it to reference radioGroup. This object is the glue that binds the four JRadioButton objects together to form the logical relationship that allows only one of the four buttons to be selected at a time. Lines 55 58 use ButtonGroup method add to associate each of the JRadioButtons with radioGroup. If more than one selected JRadioButtonobject is added to the group, the first selected JRadioButtonadded will be selected when the GUI is displayed. Class RadioButtonHandler (line 82 104) implements interface ItemListener so it can handle item events generated by the JRadioButtons. Each JRadioButton in the program has an instance of this class (handler) registered as its ItemListener. When the user clicks a JRadioButton, radioGroup turns off the previously selected JRadioButton and method itemStateChanged (line 85 102) Copyright 1992 2002 by Deitel & Associates, Inc. All Rights Reserved. 7/7/01