Buttons, Check Boxes, and Radio Buttons

Buttons, Check Boxes, and Radio Buttons

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 ActionEvents 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.

Component Architecture

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:

  • It provides facilities to manage the various different types of event listeners which can be registered to buttons, i.e., ActionListeners, ChangeListeners, and ItemListeners, and offers convenience methods through which events may be fired to them.
  • It holds the reference to the button's model. It automatically registers itself to be notified of events from the model and re-fires them to its own listeners.
  • It contains fundamental properties used by concrete implementations.

Model

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.

Events

Buttons fire several different types of events in response to different stimuli. ActionEvents are fired when buttons are clicked. ChangeEvents are fired to reflect state changes. ItemEvents are fired when a button is selected or deselected.

"Push" Buttons

"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

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.

Check Boxes

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

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.

Grouping Radio Buttons

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.