Java Reference
In-Depth Information
CHAPTER 5
■ ■ ■
Toggle Buttons
N ow that you've seen the capabilities of the relatively simple Swing components JLabel and
JButton , it's time to take a look at more active components, specifically those that can be toggled.
These so-called toggleable components— JToggleButton , JCheckBox , and JRadioButton —provide
the means for your users to select from among a set of options. These options are either on or
off, or enabled or disabled. When presented in a ButtonGroup , only one of the options in the
group can be selected at a time. To deal with this selection state, the components share a common
data model with ToggleButtonModel . Let's take a look at the data model, the components'
grouping mechanism with ButtonGroup , and the individual components.
ToggleButtonModel Class
The JToggleButton.ToggleButtonModel class is a public inner class of JToggleButton . The class
customizes the behavior of the DefaultButtonModel class, which, in turn, is an implementation
of the ButtonModel interface.
The customization affects the data models of all AbstractButton components in the
same ButtonGroup —a class explored next. In short, a ButtonGroup is a logical grouping of
AbstractButton components. At any one time, only one of the AbstractButton components
in the ButtonGroup can have the selected property of its data model set to true . The remaining
ones must be false . This does not mean that only one selected component in the group can
exist at a time. If multiple components in a ButtonGroup share a ButtonModel , multiple selected
components in the group can exist. If no components share a model, at most, the user can
select one component in the group. Once the user has selected that one component, the user
cannot interactively deselect the selection. However, programmatically, you can deselect all
group elements.
The definition of JToggleButton.ToggleButtonModel follows.
public class ToggleButtonModel extends DefaultButtonModel {
// Constructors
public ToggleButtonModel();
// Properties
public boolean isSelected();
public void setPressed(boolean newValue);
public void setSelected(boolean newvalue);
}
115
Search WWH ::




Custom Search