A "button" is a region containing text, an image, or both that fires events and/or changes state when clicked. Echo provides several different "button" components built upon a common architecture. The most fundamental of these components is the Button
component, which has been used and discussed in previous chapters of this tutorial. It has no state, and simply fires ActionEvent
s when it is pressed. The RadioButton
and CheckBox
components are buttons which have state. When clicked, they may respond by adjusting their state in addition to notifying the application of the user's action.
Echo provides a base class, AbstractButton
, which functions as the foundation upon which all button components are built. It handles a number of duties which are necessary for button components:
ActionListener
s, ChangeListener
s, and ItemListener
s, and offers convenience methods through which events may be fired to them.Buttons use a model-view-controller pattern to separate their state from their input processing and visual display code. A button's model is used only to hold information related to the selection state of the button. The ButtonModel
interface specifies the requirements for a button model. An instance of the default implementation, called DefaultButtonModel
, is created by push-style buttons when a model is not explicitly provided at construction. Toggle-style buttons use a more specialized ToggleButtonModel
class by default.
Buttons fire several different types of events in response to different stimuli. ActionEvent
s are fired when buttons are clicked. ChangeEvent
s are fired to reflect state changes. ItemEvent
s are fired when a button is selected or deselected.
"Push" buttons are used to provide the user with a means of taking an action within an application. A push button does not have a selection state. When clicked, this type of button will simply fire an ActionEvent
. Push buttons use a default model implementation, DefaultButtonModel
, by default.
Toggle buttons have state: they may be selected or deselected. Toggle buttons extend the abstract ToggleButton
class, which itself extends AbstractButton
. Toggle button components use a customized model by default, ToggleButtonModel
, which is an inner class of ToggleButton
and a derivative of DefaultButtonModel
.
A Check box is a kind of toggle button which enables the user to specify whether a single item is selected or deselected. The CheckBox
class is a direct extension of the ToggleButton
component. Check boxes are not grouped in any way--changing the state of one will have no effect on others.
Radio buttons allow a user to select a single option from within a group of options. Only one radio button within a group may be selected at any given time. Like checkboxes, a state indicator adjacent to the icon and/or text of a radio button is used to indicate the selection state of each button. Selecting a new radio button within a group will cause the currently selected radio button in the group to be deselected.
Radio buttons are different from other AbstractButton
-based components in that they require an additional class, ButtonGroup
, to operate. As its name would suggest, ButtonGroup
is used to organize radio buttons into groups. A radio button may be a member of only one group. Only one button within a group may be selected at a given time.