Previous | Next | Trail Map | Creating a User Interface | Using Components, the GUI Building Blocks


How to Use Checkboxes

The Checkbox(in the API reference documentation) class provides checkboxes -- two-state buttons that can be either "on" or "off". If you want a group of checkboxes in which only one checkbox at a time can be "on", you can add a CheckboxGroup(in the API reference documentation) object to oversee the checkboxes. Other good ways of providing groups of items the user can select are choices, lists, and menus.

Below is an applet that has two columns of checkboxes. On the left are three independent checkboxes. You can select all three of the checkboxes, if you like. On the right are three checkboxes that are coordinated by a CheckboxGroup object. The CheckboxGroup ensures that no more than one of its checkboxes is selected at a time.

Here's the whole program. Below is the code that creates both groups of checkboxes. Note that only the second, mutually-exclusive group of checkboxes is controlled by a CheckboxGroup.

Panel p1, p2;
Checkbox cb1, cb2, cb3; //independent checkboxes
Checkbox cb4, cb5, cb6; //only one of these three can be selected
CheckboxGroup cbg;

cb1 = new Checkbox();
cb1.setLabel("Checkbox 1");
cb2 = new Checkbox("Checkbox 2");
cb3 = new Checkbox("Checkbox 3");
cb3.setState(true);

. . .

cbg = new CheckboxGroup();
cb4 = new Checkbox("Checkbox 4", cbg, false);
cb5 = new Checkbox("Checkbox 5", cbg, false);
cb6 = new Checkbox("Checkbox 6", cbg, false);


Previous | Next | Trail Map | Creating a User Interface | Using Components, the GUI Building Blocks