CheckboxGroup
The CheckboxGroup
lets multiple checkboxes work together to provide a mutually exclusion choice (at most one Checkbox
can be selected at a time). Because the CheckboxGroup
is neither a Component
nor a Container
, you should normally put all the Checkbox
components associated with a CheckboxGroup
in their own Panel
(or other Container
). The LayoutManager
of the Panel
should be GridLayout (0, 1)
if you want them in one column. Figure 9.5 shows both a good way and bad way of positioning a set of Checkbox
items in a CheckboxGroup
. The image on the left is preferred because the user can sense that the items are grouped; the image on the right suggests three levels of different checkboxes and can therefore surprise the user when checkboxes are deselected.
Figure 9.5: Straightforward and confusing layouts of Checkbox components
CheckboxGroup Methods
Constructors- public CheckboxGroup ()
- This constructor creates an instance of
CheckboxGroup
.
- public int getSelectedCheckbox ()
public Checkbox getCurrent () - The
getSelectedCheckbox()
method returns theCheckbox
within theCheckboxGroup
whose value istrue
. If no item is selected,null
is returned.getCurrent()
is the Java 1.0 name for this method. - public synchronized void setSelectedCheckbox (Checkbox checkbox)
public synchronized void setCurrent (Checkbox checkbox) - The
setSelectedCheckbox()
method makescheckbox
the currently selectedCheckbox
within theCheckboxGroup
. Ifcheckbox
isnull
, the method deselects all the items in theCheckboxGroup
. Ifcheckbox
is not within theCheckboxGroup
, nothing happens.setCurrent()
is the Java 1.0 name for this method. - public String toString ()
- The
toString()
method ofCheckboxGroup
creates aString
representation of the current choice (as returned bygetSelectedCheckbox()
). Using the "straightforward" layout in Figure 9.5 as an example, the results would be:java.awt.CheckboxGroup[current=java.awt.Checkbox[0,31,85x21, label=Helvetica,state=true]]
If there is no currently selected item, the results within the square brackets would be
current=null
.