13.5 Checkbox Groups (Radio Buttons)

If you combine checkboxes into a CheckboxGroup, you get versions with a different graphical look where only one can be selected at any one time. Selecting a new entry results in the previously selected entry becoming unselected. Just as with radio button input forms in HTML (see Section 17.3, "FORM Input Elements"), there is no requirement that all the checkboxes in a given group be placed near each other. However, this is almost always what you want. This is typically accomplished by grouping them in a Panel or choosing an appropriate layout manager. The most common usage is to first create the CheckboxGroup object, then to create the checkboxes that belong to the group. These checkboxes will also contain an argument specifying if they are initially checked. This is summarized as follows:

      CheckboxGroup cbGroup = new CheckboxGroup();
      Checkbox cb1 = new Checkbox("...", cbGroup, true);
      add(cb1);
      Checkbox cb2 = new Checkbox("...", cbGroup, false);
      add(cb2);
      ...

Constructors

CheckboxGroup

public CheckboxGroup()

This creates a non-graphical object used as a "tag" to group checkboxes together into a set of radio buttons. Checkboxes that are associated with a tag will look and act like radio buttons rather than like normal checkboxes. Only one checkbox associated with a particular tag can be selected at any given time.

Checkbox

public Checkbox(String label, CheckboxGroup group, boolean state)

This creates a radio button associated with the specified group, with the given label and initial state. If you specify an initial state of true for more than one Checkbox in a group, the last one will be shown selected.

public Checkbox(String label, boolean state, CheckboxGroup group) [Java 1.1]

This JDK 1.1 constructor has the same effect as the above, but takes the final two arguments in the opposite order.

Example

Listing 13.16 presents an applet that illustrates the difference between normal checkboxes and those made into radio buttons via a CheckboxGroup. The left column shows radio buttons; the right shows regular checkboxes. Figures 13-11, 13-12, and 13-13 show the results on Windows 95, MacOS, and Solaris, respectively.

Listing 13.16 CheckboxGroups.java

import java.applet.Applet;
import java.awt.*;

public class CheckboxGroups extends Applet {
  public void init() {
    setLayout(new GridLayout(4, 2));
    setBackground(Color.lightGray);
    setFont(new Font("TimesRoman", Font.BOLD, 16));
    add(new Label("Flavor", Label.CENTER));
    add(new Label("Toppings", Label.CENTER));
    CheckboxGroup flavorGroup = new CheckboxGroup();
    add(new Checkbox("Vanilla", flavorGroup, true));
    add(new Checkbox("Colored Sprinkles"));
    add(new Checkbox("Chocolate", flavorGroup, false));
    add(new Checkbox("Cashews"));
    add(new Checkbox("Strawberry", flavorGroup, false));
    add(new Checkbox("Kiwi"));
  }
}

Figure 13-11 Radio buttons (left) vs. regular checkboxes (right) in Windows 95.



Figure 13-12 Radio buttons vs. regular checkboxes in MacOS.



Figure 13-13 Radio buttons vs. regular checkboxes in Solaris.



Other CheckboxGroup and Checkbox Methods

CheckboxGroup

public Checkbox getCurrent()
public Checkbox getSelectedCheckbox() [Java 1.1]

These methods return the radio button (Checkbox) that is currently selected. They return null if none is checked.

public void setCurrent(Checkbox boxToSelect)
public void setSelectedCheckbox(Checkbox boxToSelect) [Java 1.1]

This sets the radio button that is currently selected. If you specify a radio button that is not part of the current group, the method call is ignored. Supplying null as an argument results in all radio buttons in the group becoming unselected.

Checkbox

In addition to the general methods described in Section 13.4 (Checkboxes), Checkbox has the following two methods specific to CheckboxGroups.

public CheckboxGroup getCheckboxGroup()

This determines the group that the radio button is associated with.

public void setCheckboxGroup(CheckboxGroup newGroup)

This registers the checkbox with a new group.

Handling CheckboxGroup Events

Putting checkboxes into a CheckboxGroup does not change the basic way in which events are handled, so you should refer to Section 13.4 (Checkboxes) for details. However, there is one small variation. As with ungrouped checkboxes, it is common to ignore the events when they occur, and simply look up the state when it is needed. Rather than cycling through the various checkboxes in a group and checking getState on each, it is easier to call the getCurrent (getSelectedCheckbox in Java 1.1) method of the associated CheckboxGroup, which returns a reference to the currently selected Checkbox.


Continue to Section 13.6 (Choice Menus (Combo Boxes)). Return to Chapter 13 table of contents.