The J CheckBox [11] class provides support for check box buttons. You can also put check boxes in menus using the J CheckBoxMenuI t em [12] class. Because J CheckBox and J CheckBoxMenuI t eminherit from Abst r act But t on, Swing check boxes have all of the usual button characteristics, as discussed in How to Use Buttons (page 156). For example, you can specify images to be used in them. [11] J Checkbox API documentation: http://java.sun.com/j2se/1.4.2/docs/api/javax/swing/JCheckBox.html.
[12] J CheckboxMenuI t emAPI documentation: http://java.sun.com/j2se/1.4.2/docs/api/javax/swing/JCheckBoxMenuItem.html. Check boxes are similar to radio buttons, but their selection model is different, by convention. Any number of check boxes in a groupnone, some, or allcan be selected. A group of radio buttons, on the other hand, can have only one button selected. See How to Use Radio Buttons (page 311) later in this chapter. Figure 4 is a picture of an application that uses four check boxes to customize a cartoon. Figure 4. The CheckBoxDemo application.
Try This: 1. 2. Click the Chin button or press Alt-c. The Chin check box becomes unselected, and the chin disappears from the picture; the other check boxes remain selected. This application has one item listener that listens to all of the check boxes. Each time it receives an event, the application loads a new picture that reflects the current state of the check boxes. A check box generates one item event and one action event per click. Usually, you listen only for Run CheckBoxDemo using Java Web Start, or compile and run the example yourself. [13]
[13] To run CheckBoxDemo using Java Web Start, click the CheckBoxDemo link on the RunExampl es/ component s. ht ml page on the CD. You can find the source files here: J avaTut or i al / ui swi ng/ component s/ exampl e- 1dot 4/ i ndex. ht ml #CheckBoxDemo. Page 1of 4 How to Use Check Boxes 5/15/2014 mk:@MSITStore:F:\All%20J ava\J AVA%20EBOOKS\Core%20java\swings\Addison%20... item events since they let you determine whether the click selected or deselected the check box. Below is the code from CheckBoxDemo. j ava that creates the check boxes and reacts to clicks. //In initialization code:
chi nBut t on = new J CheckBox( " Chi n" ) ;
chi nBut t on. set Mnemoni c( KeyEvent . VK_C) ;
chi nBut t on. set Sel ect ed( t r ue) ;
gl assesBut t on = new J CheckBox( " Gl asses" ) ;
gl assesBut t on. set Mnemoni c( KeyEvent . VK_G) ;
gl assesBut t on. set Sel ect ed( t r ue) ;
hai r But t on = new J CheckBox( " Hai r " ) ;
hai r But t on. set Mnemoni c( KeyEvent . VK_H) ;
hai r But t on. set Sel ect ed( t r ue) ;
t eet hBut t on = new J CheckBox( " Teet h" ) ;
t eet hBut t on. set Mnemoni c( KeyEvent . VK_T) ;
t eet hBut t on. set Sel ect ed( t r ue) ;
/ / Regi st er a l i st ener f or t he check boxes.
chi nBut t on. addI t emLi st ener ( t hi s) ;
gl assesBut t on. addI t emLi st ener ( t hi s) ;
hai r But t on. addI t emLi st ener ( t hi s) ;
t eet hBut t on. addI t emLi st ener ( t hi s) ;
. .
publ i c voi d i t emSt at eChanged( I t emEvent e) {
. . .
Obj ect sour ce = e. get I t emSel ect abl e( ) ;
i f ( sour ce == chi nBut t on) {
Page 2of 4 How to Use Check Boxes 5/15/2014 mk:@MSITStore:F:\All%20J ava\J AVA%20EBOOKS\Core%20java\swings\Addison%20... //...make a note of it...
} el se i f ( sour ce == gl assesBut t on) {
//...make a note of it...
} el se i f ( sour ce == hai r But t on) {
//...make a note of it...
} el se i f ( sour ce == t eet hBut t on) {
//...make a note of it...
}
i f ( e. get St at eChange( ) == I t emEvent . DESELECTED)
//...make a note of it...
. . .
updat ePi ct ur e( ) ;
}
The Check Box API Table 6 lists the commonly used check box-related API. Check boxes also use the common button API, which is listed in the tables in The Button API (page 160). Other methods you might call, such as set Font and set For egr ound, are listed in the API tables in The JComponent Class (page 53) in Chapter 3. Also refer to the API documentation for J CheckBox [14] and J CheckBoxMenuI t em. [15]
[14] J Checkbox API documentation: http://java.sun.com/j2se/1.4.2/docs/api/javax/swing/JCheckBox.html.
[15] J CheckboxMenuI t emAPI documentation: http://java.sun.com/j2se/1.4.2/docs/api/javax/swing/JCheckBoxMenuItem.html. Table 6. Check Box Constructors Constructor Purpose J CheckBox( Act i on)
J CheckBox( St r i ng)
J CheckBox( St r i ng, bool ean)
J CheckBox( I con)
J CheckBox( I con, bool ean)
J CheckBox( St r i ng, I con)
Create a J CheckBox instance. The string argument specifies the text, if any, that the check box should display. Similarly, the I con argument specifies the image that should be used instead of the look and feel's default check box image. Specifying the boolean argument as true initializes the check box to be selected. If the boolean argument is absent or false, then the check box is initially unselected. The J CheckBox( Act i on) constructor was introduced in 1.3. Page 3of 4 How to Use Check Boxes 5/15/2014 mk:@MSITStore:F:\All%20J ava\J AVA%20EBOOKS\Core%20java\swings\Addison%20... Examples That Use Check Boxes The following table lists two examples that use check boxes. J CheckBox( St r i ng, I con, bool ean)
J CheckBox( )
J CheckBoxMenuI t em( Act i on)
J CheckBoxMenuI t em( St r i ng)
J CheckBoxMenuI t em( St r i ng, bool ean)
J CheckBoxMenuI t em( I con)
J CheckBoxMenuI t em( St r i ng, I con)
J CheckBoxMenuI t em( St r i ng, I con, bool ean)
J CheckBoxMenuI t em( )
Create a J CheckBoxMenuI t eminstance. The arguments are interpreted in the same way as the arguments to the J CheckBox constructors, except that any specified icon is shown in addition to the normal check box icon. The J CheckBoxMenuI t em( Act i on) constructor was introduced in 1.3. Example Where Described Notes CheckBoxDemo How to Use Check Boxes (page 163) Uses check box buttons to determine which of 16 images it should display. Act i onDemo How to Use Actions (page 513) Uses check box menu items to set the state of the program. < Day Day Up > Page 4of 4 How to Use Check Boxes 5/15/2014 mk:@MSITStore:F:\All%20J ava\J AVA%20EBOOKS\Core%20java\swings\Addison%20...