boolean
value. Each Checkbox
has a label that should be used to tell the user what the Checkbox
represents. For instance a Checkbox
with the label "Anchovies" would be checked if the user wants anchovies on their pizza and unchecked if they don't.
Checkboxes are often used to select from a list of possible choices when as few selections as zero or as many as everything on the list may be made. Adding a Checkbox
to an applet is simple. Just declare it, construct it and add it.
Checkbox c;
c = new Checkbox("Pepperoni"));
add(c);
As usual these steps may be combined into the single line
add(new Checkbox("Pepperoni"));
By default check boxes are unchecked when created. If you want a Checkbox
to start life checked, use the following constructor instead:
add(new Checkbox("Pepperoni", null, true));
The null
is a reference to a CheckboxGroup
. Passing null
for this argument says that this Checkbox
does not belong to a CheckboxGroup
.
Every Checkbox
has a boolean
value, either true
or false
. When the Checkbox
is checked that value is true
. When it is unchecked that value is false. You access this value using the Checkbox
's getState()
and setState(boolean b)
methods. For example
private void handleCheckbox(Checkbox c) {
if (c.getState()) price += 0.50f;
else price -= 0.50f;
}