Previous | Next | Trail Map | Creating a User Interface | Using the JFC/Swing Packages

How to Use Check Boxes

The Swing release supports check box buttons with the JCheckBox(in the API reference documentation) class. Swing also supports check boxes in menus, using the JCheckBoxMenuItem(in the API reference documentation) class. Because JCheckBox and JCheckBoxMenuItem inherit from AbstractButton, Swing check boxes have all the usual button characteristics, as discussed in How to Use Buttons. For example, you can specify images to be used in check boxes.

Check boxes are similar to radio buttons, but their selection model is different, by convention. Any number of check boxes in a group -- none, some, or all -- can be selected. A group of radio buttons, on the other hand, can have only one button selected.


Note: In Swing 1.0.2, buttons ignore their mnemonics (key accelerators). This bug is fixed in Swing 1.0.3.

Here is a picture of an application that uses four check boxes to customize a cartoon:

NOT a tutorial reader!


Try this:
  1. Compile and run the application. The source file is CheckBoxDemo.java. You will also need the 16 image files in the example-swing/images directory that begin with "geek". To get the image files, you can download the swing lesson.
    See Getting Started with Swing if you need help.
  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 the check boxes. Each time the item listener 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 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 in the previous example and reacts to clicks.

//In initialization code:
    chinButton = new JCheckBox("Chin");
    chinButton.setMnemonic('c'); 
    chinButton.setSelected(true);

    glassesButton = new JCheckBox("Glasses");
    glassesButton.setMnemonic('g'); 
    glassesButton.setSelected(true);

    hairButton = new JCheckBox("Hair");
    hairButton.setMnemonic('h'); 
    hairButton.setSelected(true);

    teethButton = new JCheckBox("Teeth");
    teethButton.setMnemonic('t'); 
    teethButton.setSelected(true);

    // Register a listener for the check boxes.
    CheckBoxListener myListener = new CheckBoxListener();
    chinButton.addItemListener(myListener);
    glassesButton.addItemListener(myListener);
    hairButton.addItemListener(myListener);
    teethButton.addItemListener(myListener);
...
class CheckBoxListener implements ItemListener {
    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...
        picture.setIcon(/* new icon */);
        ...
    }
}

The Check Box API

See The Button API for information on the AbstractButton API that JCheckBox and JCheckBoxMenuItem inherit. The AbstractButton methods that you're most likely to use are setMnemonic, addItemListener, setSelected, and isSelected. The only API defined by JCheckBox and JCheckBoxMenuItem that you're likely to use are the constructors.

Check Box Constructors
Constructor Purpose
JCheckBox(String)
JCheckBox(String, boolean)
JCheckBox(Icon)
JCheckBox(Icon, boolean)
JCheckBox(String, Icon)
JCheckBox(String, Icon, boolean)
JCheckBox()
Create a JCheckBox instance. The string argument specifies the text, if any, that the check box should display. Similarly, the Icon 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.
JCheckBoxMenuItem(String)
JCheckBoxMenuItem(String, boolean)
JCheckBoxMenuItem(Icon)
JCheckBoxMenuItem(String, Icon)
JCheckBoxMenuItem(String, Icon, boolean)
JCheckBoxMenuItem()
Create a JCheckBoxMenuItem instance. The arguments are interpreted in the same way as the arguments to the JCheckBox constructors.

Examples that Use Check Boxes

The following examples use check boxes either as buttons or as menu items.

Example Where Described Notes
CheckBoxDemo.java This page. Uses check box buttons to determine which of 16 images it should display.
ActionDemo.java How to Use Actions Uses check box menu items to set the state of the program.


Previous | Next | Trail Map | Creating a User Interface | Using the JFC/Swing Packages