Java Reference
In-Depth Information
All the Swing component classes are defined in the
javax.swing
package and have class names that be-
gin with
J
. There are quite a few Swing components, so I'll give you an overview of the most used classes
and how they relate to one another. I will go into the detail of particular Swing components when I use them
in examples.
Buttons
The Swing button classes in the
javax.swing
package define various kinds of buttons operated by clicking
with a mouse. The button classes have the
AbstractButton
class as a base, as shown in
Figure 17-10
.
The
JButton
class defines a regular pushbutton that you would use as a dialog button — OK and Cancel
buttons, for example, that you are sure to have seen and used — or in a toolbar, where the buttons might
provide alternatives to using menu items. There are two subclasses of
JButton
,
BasicArrowButton
that is
a button with an arrow on it and
MetalComboBoxButton
, which is a helper class for creating the metal look-
and-feel. I won't be going into these in detail.
The
JToolBar
class is used in conjunction with the
JButton
class to create a toolbar containing buttons.
A toolbar is dockable without any additional programming effort on your part, as you later see.
The
JToggleButton
class defines a two-state button, pressed or not, and two more specialized versions
are defined by
JCheckBox
and
JRadioButton
. Radio buttons defined as
JRadioButton
objects generally
operate in a group so that only one button can be in the pressed state at any one time. This grouping is estab-
lished by adding the
JRadioButton
object to a
ButtonGroup
object that takes care of the state of the buttons
in the group.
A
JCheckBox
object is a button with a square checkbox to the left. Clicking on the checkbox changes its
state from checked to unchecked or vice versa.
All the buttons can be displayed with a text label, an icon, or both.
Menus
The Swing components include support for pop-up or context menus as well as menu bars. The classes de-
fining elements of a menu are shown in
Figure 17-11
.