Java Reference
In-Depth Information
tialized with
"Serif"
(a generic font name that represents a font such as Times and is sup-
ported on all Java platforms),
Font.PLAIN
style and
14
-point size. Next, lines 28-29 create
two
JCheckBox
objects. The
String
passed to the
JCheckBox
constructor is the
checkbox
label
that appears to the right of the
JCheckBox
by default.
When the user clicks a
JCheckBox
, an
ItemEvent
occurs. This event can be handled
by an
ItemListener
object, which
must
implement method
itemStateChanged
. In this
example, the event handling is performed by an instance of
private
inner class
CheckBox-
Handler
(lines 40-60). Lines 34-36 create an instance of class
CheckBoxHandler
and reg-
ister it with method
addItemListener
as the listener for both the
JCheckBox
objects.
CheckBoxHandler
method
itemStateChanged
(lines 43-59) is called when the user
clicks the either
boldJCheckBox
or
italicJCheckBox
. In this example, we do not deter-
mine which
JCheckBox
was clicked—we use both of their states to determine the font to
display. Line 49 uses
JCheckBox
method
isSelected
to determine if both
JCheckBox
es are
selected. If so, line 50 creates a bold italic font by adding the
Font
constants
Font.BOLD
and
Font.ITALIC
for the font-style argument of the
Font
constructor. Line 51 determines
whether the
boldJCheckBox
is selected, and if so line 52 creates a bold font. Line 53 deter-
mines whether the
italicJCheckBox
is selected, and if so line 54 creates an italic font. If
none of the preceding conditions are true, line 56 creates a plain font using the
Font
con-
stant
Font.PLAIN
. Finally, line 58 sets
textField
's new font, which changes the font in
the
JTextField
on the screen.
Relationship Between an Inner Class and Its Top-Level Class
Class
CheckBoxHandler
used variables
boldJCheckBox
(lines 49 and 51),
italicJCheck-
Box
(lines 49 and 53) and
textField
(line 58) even though they are
not
declared in the
inner class. Recall that an
inner class
has a special relationship with its
top-level class
—it's
allowed to access
all
the variables and methods of the top-level class.
CheckBoxHandler
method
itemStateChanged
(line 43-59) uses this relationship to determine which
JCheckBox
es are checked and to set the font on the
JTextField
. Notice that none of the
code in inner class
CheckBoxHandler
requires an explicit reference to the top-level class
object.
Radio buttons
(declared with class
JRadioButton
) are similar to checkboxes in that they
have two states—
selected
and
not selected
(also called
deselected
). However, radio buttons
normally appear as a
group
in which only
one
button can be selected at a time (see the out-
put of Fig. 12.20). Radio buttons are used to represent
mutually exclusive
options
(i.e.,
multiple options in the group
cannot
be selected at the same time). The logical relationship
between radio buttons is maintained by a
ButtonGroup
object (package
javax.swing
),
which itself is
not
a GUI component. A
ButtonGroup
object organizes a group of buttons
and is
not
itself displayed in a user interface. Rather, the individual
JRadioButton
objects
from the group are displayed in the GUI.
The application of Figs. 12.19-12.20 is similar to that of Figs. 12.17-12.18. The user
can alter the font style of a
JTextField
's text. The application uses radio buttons that
permit only a single font style in the group to be selected at a time. Class
RadioButtonTest
(Fig. 12.20) contains the
main
method that executes this application.