Java Reference
In-Depth Information
As you saw earlier, semantic events relate to operations on the components in the GUI for your program. If
you select a menu item or click a button, for example, a semantic event is generated. Three classes represent
the basic semantic events you deal with most of the time, and they are derived from the
AWTEvent
class, as
shown in
Figure 18-4
.
An
ActionEvent
is generated when you perform an action on a component such as clicking on a menu
item or a button. An
ItemEvent
occurs when a component is selected or deselected, and an
Adjust-
mentEvent
is produced when an adjustable object, such as a scrollbar, is adjusted.
Different kinds of components can produce different kinds of semantic events. Some of the components
that can originate the three types of event in
Figure 18-4
are the following:
•
ActionEvent
Buttons:
JButton
,
JToggleButton
,
JCheckBox
Menus:
JMenuItem
,
JMenu
,
JCheckBoxMenuItem
,
JRadioButtonMenuItem
•
ItemEvent
Buttons:
JButton
,
JToggleButton
,
JCheckBox
Menus:
JMenuItem
,
JMenu
,
JCheckBoxMenuItem
,
JRadioButtonMenuItem
•
AdjustmentEvent
Scrollbar:
JScrollbar
These three types of event are also generated by the old AWT components, but I won't go into these
because you'll only use Swing components. Of course, any class you derive from these component classes
to define your own customized components can be the source of the event that the base class generates. If
you define your own class for buttons —
MyFancyButton
, say — your class has
JButton
as a base class
and inherits all of the methods from the
JButton
class, and objects of your class originate events of type
ActionEvent
and
ItemEvent
.
As with low-level events, the most convenient way to handle semantic events is to use listeners, so I delve
into the listener interfaces for semantic events next.
Semantic Event Listeners