Java Reference
In-Depth Information
Special debugging support for graphics, providing component rendering in slow motion so
you can see what is happening.
❑
Component classes can be easily extended to create your own custom components.
❑
All the Swing component classes are defined in the
javax.swing
package and have class names that
begin with
J
. There are quite a few Swing components, so we'll get an overview of what's available and
how the classes relate to one another and then go into the detail of particular components when we use
them in examples.
Buttons
The Swing button classes define various kinds of buttons operated by clicking with a mouse. The button
classes have
AbstractButton
as a base, as shown below.
The
JButton
class defines a regular pushbutton that you would use as a dialog button or in a toolbar.
This is an example of a
JButton
object. This component has a border of type
BevelBorder
added to it.
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 we will see.
JToggleButton
defines a two-state button, pressed or not, and there are two more specialized
versions 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 established by adding the
JRadioButton
object to a
ButtonGroup
object that takes
care of the state of the buttons in the group.
This is an example of a
JCheckBox
object. 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
defining elements of a menu are shown overleaf.