Java Reference
In-Depth Information
Constructing Checkboxes
In previous chapters, the applets used components from the java.awt
package, such as Labels, TextFields, and Buttons, to create the user interface. In
this chapter, you will create a constructor for a Checkbox component that is used
in the user interface.
Java has two different kinds of Checkboxes. The first is a traditional
Checkbox , which displays as a small square with a caption. When selected, the
Checkbox displays a check mark. The Checkbox has the toggled value of on or
off, depending on whether the check mark displays. The Checkbox is an inde-
pendent component that allows the user to select it, regardless of other choices
in the interface.
The second kind of Checkbox, called a CheckboxGroup , is used to group
together several Checkbox components. When a Checkbox component is used
in a CheckboxGroup, it displays as a small circle or option button. Exactly one
Checkbox in a CheckboxGroup can be selected, or checked, at any given time.
When the user clicks one of the components, the others automatically become
deselected.
Table 4-13 displays the general form of the Checkbox() and
CheckboxGroup() methods used to add a Checkbox or CheckboxGroup
component to the user interface. For a single Checkbox component, the
Checkbox() method takes a String argument that represents the caption , or
label, you want to display beside the Checkbox. When using mutually exclusive
Checkboxes in a CheckboxGroup, the Checkbox() method takes three arguments:
the caption, the state, and the name of the CheckboxGroup(). The state is true
or false, depending on whether you want the Checkbox in the CheckboxGroup
checked for true, or not checked for false. Because it is a mutually exclusive
grouping, only one Checkbox in the CheckboxGroup can have its state value set
to true at any given time. The CheckboxGroup name is the identifier previously
used when the CheckboxGroup was constructed. The CheckboxGroup name
assigns the Checkbox as a member of the particular CheckboxGroup.
Table 4-13
Checkbox() and CheckboxGroup() Methods
General form:
//Checkbox
Checkbox variableName = new Checkbox(“caption”);
//grouped Checkbox
CheckboxGroup variableName = new CheckboxGroup();
Checkbox variableName = new Checkbox(“caption”, state, GroupName);
Purpose:
Component allows user to select on or off in a non-grouped Checkbox or to select one from
many in a grouped Checkbox. Checkboxes toggle checked and unchecked based on a user's
click. Grouped Checkboxes are mutually exclusive. Selecting one from among the group
deselects all others.
Examples:
1. Checkbox mayoBox = new Checkbox("Mayo");
Checkbox ketchupBox = new Checkbox("Ketchup");
Checkbox mustardBox = new Checkbox("Mustard");
2. CheckboxGroup sizeGroup = new CheckboxGroup();
Checkbox smallOpt = new Checkbox("Small", false, sizeGroup);
Checkbox mediumOpt = new Checkbox("Medium", false, sizeGroup);
Checkbox largeOpt = new Checkbox("Large", true, sizeGroup);
 
Search WWH ::




Custom Search