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


How to Use Check Boxes

The Swing release supports check boxes with the JCheckBox(in the API reference documentation) and ButtonGroup(in the API reference documentation) classes. Because JCheckBox inherits 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.

Here is a picture of an application that has two check boxes:


[We'll try to make the example more interesting...]


Try this:
  1. Compile and run the application. The source file is CheckBoxDemo.java.
    See Getting Started with Swing if you need help.
  2. Click Button 2.
    Button 2 becomes selected. Button 1 remains selected.
  3. Look at the messages displayed at the standard output.
    This application registers a listener for each kind of event a button can send -- action, change, and item. Each time it receives an event, the application displays a message describing the event.
  4. Click Button 2 again, and look at the messages displayed at the standard output.

A check box generates one item event and one action event per click. Usually, the only event handler a check box needs is an item listener. If you'd rather use the API associated with action events. you can use an action listener instead. You don't need to implement a change listener unless your program needs to know every time the button's appearance changes. [check all this]

Below is the code from CheckBoxDemo.java that creates the check boxes in the previous example and reacts to clicks.

[XXX GET NEWER CODE]
//In initialization code:
    // Create the buttons.
    JCheckBox firstButton = new JCheckBox(first);
    firstButton.setKeyAccelerator('1'); 
    firstButton.setActionCommand(first);
    firstButton.setSelected(true);

    JCheckBox secondButton = new JCheckBox(second);
    secondButton.setKeyAccelerator('2'); 
    secondButton.setActionCommand(second);

    // Register a listener for the check boxes.
    CheckboxListener myListener = new CheckboxListener();
    firstButton.addActionListener(myListener);
    firstButton.addChangeListener(myListener);
    firstButton.addItemListener(myListener);
    secondButton.addActionListener(myListener);
    secondButton.addChangeListener(myListener);
    secondButton.addItemListener(myListener);
. . .
class CheckboxListener implements ItemListener, //only event type needed
			       ActionListener, //for curiosity only
			       ChangeListener {  //for curiosity only
    public void itemStateChanged(ItemEvent e) {
        System.out.println("ItemEvent received: " 
    		           + e.getItem()
    		           + " is now "
    		           + ((e.getStateChange() == ItemEvent.SELECTED)?
    			      "selected.":"unselected"));
    }

    public void actionPerformed(ActionEvent e) {
        String factoryName = null;

        System.out.print("ActionEvent received: ");
        if (e.getActionCommand() == first) {
    	    System.out.println(first + " pressed.");
        } else {
    	    System.out.println(second + " pressed.");
        }
    }

    public void stateChanged(ChangeEvent e) {
        System.out.println("ChangeEvent received from: "
    		           + e.getSource());
    }
}
See How to Use Buttons for information on the AbstractButton API that JCheckBox inherits. The only API defined by JCheckBox that you're likely to use are the constructors. JCheckBox defines seven constructors: The arguments are straightforward:
String
Specifies the text that the check box should display.
Icon
Specifies the image that the check box should display. Unless you specify an image, the images defined by the look-and-feel are used.
boolean
Specifies whether the check box is selected. By default, it's false (not selected).


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