How to Use Check Boxes

 < 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.

graphics/07fig04.jpg

Try This:

  1. graphics/cd_icon.gif

    Run CheckBoxDemo using Java Web Start, or compile and run the example yourself. [13]

    [13] To run CheckBoxDemo using Java Web Start, click the CheckBoxDemo link on the RunExamples/ components .html page on the CD. You can find the source files here: JavaTutorial/uiswing/components/example-1dot4/index.html#CheckBoxDemo .

  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 of the check boxes. Each time it 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 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

Constructor

Purpose

 JCheckBox(Action) 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. The JCheckBox(Action) constructor was introduced in 1.3.

 JCheckBoxMenuItem(Action) 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, except that any specified icon is shown in addition to the normal check box icon. The JCheckBoxMenuItem(Action) constructor was introduced in 1.3.

Examples That Use Check Boxes

The following table lists two examples that use check boxes.

Example

Where Described

Notes

CheckBoxDemo

How to Use Check Boxes (page 163)

Uses check box buttons to determine which of 16 images it should display.

ActionDemo

How to Use Actions (page 513)

Uses check box menu items to set the state of the program.

 < Day Day Up > 


JFC Swing Tutorial, The. A Guide to Constructing GUIs
The JFC Swing Tutorial: A Guide to Constructing GUIs (2nd Edition)
ISBN: 0201914670
EAN: 2147483647
Year: 2004
Pages: 171

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net