Listeners Supported by Swing
Components
You can tell what kinds of events a component can fire by looking at the kinds of event listeners you can register on it. For example, the
JComboBox
class defines these listener registration
methods
:
-
addActionListener
-
addItemListener
-
addPopupMenuListener
Thus, a combo box supports action, item, and popup menu listeners in addition to the listener type support it inherits from
JComponent
.
A component fires only those events for which listeners have registered on it. For example, if an action listener is registered on a particular combo box, but the combo box has no other listeners, then the combo box will fire only action events ”no item or popup menu events.
Listeners supported by Swing components fall into two categories:
Listeners That All Swing Components Support
Because all Swing components descend from the AWT
Component
class, you can register the following listeners on any Swing component:
component listener
Listens for changes in the component's
size
, position, or visibility.
focus listener
Listens for whether the component
gained
or lost the ability to receive keyboard input.
key listener
Listens for key presses; key events are
fired
only by the component that has the current keyboard focus.
mouse listener
Listens for mouse clicks and mouse movement into or out of the component's drawing area.
mouse-motion
listener
Listens for changes in the cursor's position over the component.
mouse-wheel listener
(introduced in 1.4)
Listens for mouse wheel movement over the component.
property-change
listener
Listens for changes to various component properties, such as the component's displayed value.
Two listener types introduced in release 1.3,
HierarchyListener
and
HierarchyBoundsListener
, listen to changes to a component's containment hierarchy. These listener types aren't useful to most programs and can
generally
be ignored.
All Swing components descend from the AWT
Container
class, but many of them aren't used as containers. So, technically speaking, any Swing component can fire container events, which notify listeners that a component has been added to or removed from the container. Realistically speaking, however, only containers (such as panels and
frames
) and compound components (such as combo boxes) fire container events.
JComponent
provides support for three more listener types. You can register an
ancestor
listener
[8]
to be notified when a component's containment
ancestors
are added to or removed from a container, hidden, made visible, or moved. This listener type is an implementation detail which predated hierarchy listeners and can generally be ignored.
[8]
API documentation for
AncestorListener
is online at: http://java.sun.com/j2se/1.4.2/docs/api/javax/swing/event/AncestorListener.html.
Swing components conform to the JavaBeans component architecture. Among other things, this means that Swing components support bound and constrained properties and notify listeners of changes to the properties. We've already mentioned property change listeners. Swing components also support vetoable change listeners,
[9]
which listen for changes to constrained properties.
[9]
API documentation for
VetoableChangeListener
is online at: http://java.sun.com/j2se/1.4.2/docs/api/java/beans/VetoableChangeListener.html.
Other Listeners That Swing Components Support
Table 1 lists Swing components and the listeners that they support, not including listeners supported by all
Component
s,
Container
s, or
JComponent
s. In many cases, the events are fired directly from the component. In other cases, the events are fired from the component's data or selection model. To find out the details for the particular component and listener you're interested in, go first to the component how-to section and then, if necessary, to the listener how-to section (using the page number in parentheses).
Table 1. Swing Components and Listeners They Support
|
Component
|
Listener
|
|
action (page 646)
|
caret (page 649)
|
change (page 652)
|
document (page 661), undoable edit (page 721)
|
item (page 674)
|
list selection (page 685)
|
window (page 723)
|
other
|
|
button (page 156)
|
|
|
|
|
|
|
|
|
|
check box (page 163)
|
|
|
|
|
|
|
|
|
|
color
chooser (page 167)
|
|
|
|
|
|
|
|
|
|
combo box (page 176)
|
|
|
|
|
|
|
|
|
|
dialog (page 187)
|
|
|
|
|
|
|
|
|
|
editor pane (page 200)
|
|
|
|
|
|
|
|
hyperlink
|
|
file chooser (page 206)
|
|
|
|
|
|
|
|
|
|
formatted text field (page 221)
|
|
|
|
|
|
|
|
|
|
frame (page 236)
|
|
|
|
|
|
|
|
|
|
internal frame (page 245)
|
|
|
|
|
|
|
|
internal frame (page 245)
|
|
list (page 267)
|
|
|
|
|
|
|
|
list data (page 682)
|
|
menu (page 277)
|
|
|
|
|
|
|
|
menu
|
|
menu item (page 277)
|
|
|
|
|
|
|
|
menu key, menu drag mouse
|
|
option pane (page 187)
|
|
|
|
|
|
|
|
|
|
password field (page 423)
|
|
|
|
|
|
|
|
|
|
popup menu (page 277)
|
|
|
|
|
|
|
|
popup menu
|
|
progress bar (page 300)
|
|
|
|
|
|
|
|
|
|
radio button (page 311)
|
|
|
|
|
|
|
|
|
|
slider (page 348)
|
|
|
|
|
|
|
|
|
|
spinner (page 357)
|
|
|
|
|
|
|
|
|
|
tabbed pane (page 382)
|
|
|
|
|
|
|
|
|
|
table (page 388)
|
|
|
|
|
|
|
|
table model (page 388), table column model,
cell
editor
|
|
text area (page 418)
|
|
|
|
|
|
|
|
|
|
text field (page 423)
|
|
|
|
|
|
|
|
|
|
text pane (page 60)
|
|
|
|
|
|
|
|
hyperlink
|
|
toggle button (page 156)
|
|
|
|
|
|
|
|
|
|
tree (page 437)
|
|
|
|
|
|
|
|
tree expansion (page 710), tree will expand (page 718), tree model (page 713), tree selection (page 715)
|
|
viewport
used by scrollpane
(page 325)
|
|
|
|
|
|
|
|
|
|