Java Reference
In-Depth Information
The item event is received in ie .
To obtain a reference to the item that changed, call getItem( ) on the ItemEvent object.
This method is shown here:
Object getItem( )
The reference returned must be cast to the component class being handled, which in this
case is JCheckBox .
You can obtain the text associated with a check box by calling getText( ) . You can set
the text after a check box is created by calling setText( ) . These methods work the same as
they do for JButton , described earlier.
The easiest way to determine the state of a check box is to call the isSelected( ) method.
It is shown here:
boolean isSelected( )
It returns true if the check box is selected and false otherwise.
The following program demonstrates check boxes. It creates three check boxes called
Alpha, Beta, and Gamma. Each time the state of a box is changed, the current action is dis-
played. Also, the list of all currently selected check boxes is displayed. Sample output is
shown in Figure 16-4 .
Figure 16-4 Sample output from the CBDemo program
 
Search WWH ::




Custom Search