The JFC Swing Tutorial: A Guide to Constructing GUIs (2nd Edition)
< Day Day Up > |
The JCheckBox [11] class provides support for check box buttons . You can also put check boxes in menus using the JCheckBoxMenuItem [12] class. Because JCheckBox and JCheckBoxMenuItem inherit from AbstractButton , 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] JCheckbox API documentation: http://java.sun.com/j2se/1.4.2/docs/api/javax/swing/JCheckBox.html. [12] JCheckboxMenuItem API 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:
A check box generates one item event and one action event per click. Usually, you listen only for item events since they let you determine whether the click selected or deselected the check box. Below is the code from CheckBoxDemo.java that creates the check boxes and reacts to clicks. //In initialization code: chinButton = new JCheckBox("Chin"); chinButton.setMnemonic(KeyEvent.VK_C); chinButton.setSelected(true); glassesButton = new JCheckBox("Glasses"); glassesButton.setMnemonic(KeyEvent.VK_G); glassesButton.setSelected(true); hairButton = new JCheckBox("Hair"); hairButton.setMnemonic(KeyEvent.VK_H); hairButton.setSelected(true); teethButton = new JCheckBox("Teeth"); teethButton.setMnemonic(KeyEvent.VK_T); teethButton.setSelected(true); //Register a listener for the check boxes. chinButton.addItemListener(this); glassesButton.addItemListener(this); hairButton.addItemListener(this); teethButton.addItemListener(this); .. public void itemStateChanged(ItemEvent e) { ... Object source = e.getItemSelectable(); if (source == chinButton) { //...make a note of it... } else if (source == glassesButton) { //...make a note of it... } else if (source == hairButton) { //...make a note of it... } else if (source == teethButton) { //...make a note of it... } if (e.getStateChange() == ItemEvent.DESELECTED) //...make a note of it... ... updatePicture(); } 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 setFont and setForeground , are listed in the API tables in The JComponent Class (page 53) in Chapter 3. Also refer to the API documentation for JCheckBox [14] and JCheckBoxMenuItem . [15] [14] JCheckbox API documentation: http://java.sun.com/j2se/1.4.2/docs/api/javax/swing/JCheckBox.html. [15] JCheckboxMenuItem API documentation: http://java.sun.com/j2se/1.4.2/docs/api/javax/swing/JCheckBoxMenuItem.html. Table 6. Check Box Constructors
Examples That Use Check Boxes
The following table lists two examples that use check boxes.
|
< Day Day Up > |