Previous | Next | Trail Map | To 1.1 -- And Beyond! | GUI Changes: The AWT Grows Up


How to Use Checkboxes

The Swing release supports checkboxes with the JCheckbox(in the API reference documentation) and ButtonGroup(in the API reference documentation) classes. Because JCheckbox inherits from AbstractButton, Swing checkboxes have all the usual button characteristics, as discussed in How to Use Buttons. For example, you can specify images to be used in checkboxes.

Here is a picture of an application that has two checkboxes:


[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 checkbox generates one item event and one action event per click. Usually, the only event handler a checkbox 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 checkboxes in the previous example and reacts to clicks.

//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 checkboxes.
    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 checkbox should display.
Icon
Specifies the image that the checkbox should display. Unless you specify an image, the images defined by the look-and-feel are used.
boolean
Specifies whether the checkbox is selected. By default, it's false (not selected).


Previous | Next | Trail Map | To 1.1 -- And Beyond! | GUI Changes: The AWT Grows Up