Java Reference
In-Depth Information
JToggleButton Class
The
JToggleButton
is the first of the toggleable components. It's discussed first because it's
the parent class of the two other components that are not menu-oriented:
JCheckBox
and
JRadioButton
. The
JToggleButton
is like a
JButton
that stays depressed when selected, instead
of bouncing back to an unselected state. To deselect the selected component, you must reselect it.
JToggleButton
isn't a commonly used component, but you might find it useful on a toolbar,
such as in Microsoft Word (for paragraph alignment, among other instances) or in a file dialog
box, as shown in the upper-right corner of Figure 5-2.
*4OGGLE"UTTON
Figure 5-2.
Sample JToggleButton components from file chooser
Defining the
JToggleButton
structure are two objects that customize the
AbstractButton
parent class:
ToggleButtonModel
and
ToggleButtonUI
. The
ToggleButtonModel
class represents
a customized
ButtonModel
data model for the component, whereas
ToggleButtonUI
is the user
interface delegate.
Now that you know about the different pieces of a
JToggleButton
, let's find out how to
use them.
Creating JToggleButton Components
Eight constructors are available for
JToggleButton
:
public JToggleButton()
JToggleButton aToggleButton = new JToggleButton();
public JToggleButton(Icon icon)
JToggleButton aToggleButton = new JToggleButton(new DiamondIcon(Color.PINK))