Java Reference
In-Depth Information
Both the
JCheckBox
and
JRadioButton
classes have several useful methods inherited
from a common superclass:
setSelected(
boolean
)
—Select the component if the argument is
true
and dese-
lect it otherwise.
n
isSelected()
—Return a
boolean
indicating whether the component is currently
selected.
n
The following constructors are available for the
JCheckBox
class:
JCheckBox(
String
)
—A check box with the specified text label
n
JCheckBox(
String
,
boolean
)
—A check box with the specified text label that is
selected if the second argument is
true
n
JCheckBox(
Icon
)
—A check box with the specified graphical icon
n
JCheckBox(
Icon
,
boolean
)
—A check box with the specified graphical icon that
is selected if the second argument is
true
n
JCheckBox(
String
,
Icon
)
—A check box with the specified text label and graphi-
cal icon
n
JCheckBox(
String
,
Icon
,
boolean
)
—A check box with the specified text label
and graphical icon that is selected if the third argument is
true
n
The
JRadioButton
class has constructors with the same arguments and functionality.
Check boxes and radio buttons by themselves are
nonexclusive
, meaning that if you have
five check boxes in a container, all five can be checked or unchecked at the same time.
To make them exclusive, as radio buttons should be, you must organize related compo-
nents into groups.
To organize several radio buttons into a group, allowing only one to be selected at a time,
create a
ButtonGroup
class object, as demonstrated in the following statement:
ButtonGroup choice = new ButtonGroup();
The
ButtonGroup
object keeps track of all radio buttons in its group. Call the group's
add(
Component
)
method to add the specified component to the group.
The following example creates a group and two radio buttons that belong to it:
ButtonGroup saveFormat = new ButtonGroup();
JRadioButton s1 = new JRadioButton(“OPML”, false);
saveFormat.add(s1);
JRadioButton s2 = new JRadioButton(“XML”, true);
saveFormat.add(s2);