GUI Changes: The AWT Grows Up |
The Swing release supports checkboxes with theJCheckbox
andButtonGroup
classes. BecauseJCheckbox
inherits fromAbstractButton
, 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:
- Compile and run the application. The source file is
CheckboxDemo.java
.
See Getting Started with Swing if you need help.- Click Button 2.
Button 2 becomes selected. Button 1 remains selected.- 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.- 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.See How to Use Buttons for information on the//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()); } }AbstractButton
API thatJCheckbox
inherits. The only API defined byJCheckbox
that you're likely to use are the constructors.JCheckbox
defines seven constructors:The arguments are straightforward:
JCheckbox(String)
JCheckbox(String, boolean)
JCheckbox(Icon)
JCheckbox(Icon, boolean)
JCheckbox(String, Icon)
JCheckbox(String, Icon, boolean)
JCheckbox()
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).
GUI Changes: The AWT Grows Up |