Java Reference
In-Depth Information
occurs. The
MouseEvent
's event ID determines which of the several mouse event-handling
methods are called. All these decisions are handled for you by the GUI components. All you
need to do is register an event handler for the particular event type that your application
requires, and the GUI component will ensure that the event handler's appropriate method
gets called when the event occurs. We discuss other event types and event-listener interfaces
as they're needed with each new component we introduce.
Performance Tip 12.1
GUIs should always remain responsive to the user. Performing a long-running task in an
event handler prevents the user from interacting with the GUI until that task completes.
Section 23.11 demonstrates techniques prevent such problems.
A
button
is a component the user clicks to trigger a specific action. A Java application can
use several types of buttons, including
command buttons
,
checkboxes
,
toggle buttons
and
radio buttons
. Figure 12.14 shows the inheritance hierarchy of the Swing buttons we cov-
er in this chapter. As you can see, all the button types are subclasses of
AbstractButton
(package
javax.swing
), which declares the common features of Swing buttons. In this
section, we concentrate on buttons that are typically used to initiate a command.
JComponent
AbstractButton
JButton
JToggleButton
JCheckBox
JRadioButton
Fig. 12.14
|
Swing button hierarchy.
A
command button
(see Fig. 12.16's output) generates an
ActionEvent
when the user
clicks it. Command buttons are created with class
JButton
. The text on the face of a
JButton
is called a
button label
.
Look-and-Feel Observation 12.8
The text on buttons typically uses book-title capitalization.
Look-and-Feel Observation 12.9
A GUI can have many
JButton
s, but each button label should be unique in the portion
of the GUI that's currently displayed. Having more than one
JButton
with the same label
makes the
JButton
s ambiguous to the user.