The JavaTM Tutorial
Previous Page Lesson Contents Next Page Start of Tutorial > Start of Trail > Start of Lesson Search
Feedback Form

Trail: Creating a GUI with JFC/Swing
Lesson: Writing Event Listeners

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 Component class defines these listener registration methods: Thus, every component supports component, focus, key, mouse, and mouse-motion listeners. However, a component fires only those events for which listeners have registered on it. For example, if a mouse listener is registered on a particular component, but the component has no other listeners, then the component will fire only mouse events--no component, focus, key, or mouse-motion 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 events
Listens for mouse clicks and mouse movement into or out of the component's drawing area.
mouse-motion events
Listens for changes in the cursor's position over the component.
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(in the API reference documentation) 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 and can generally be ignored. Swing components are JavaBeans-compliant. Among other things, this means that Swing Components support bound and constrained properties and notify listeners of changes to the properties. Property change listeners(in the API reference documentation) listen for changes to bound properties and vetoable change listeners(in the API reference documentation) listen for changes to constrained properties. These listeners are used primarily by Beans-aware builder tools. For more information about Beans Properties refer to Properties(in the Creating a GUI with JFC/Swing trail).

Other Listeners that Swing Components Support

The following table lists Swing components and the listeners that they support. 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.

Component Listener
action caret change document,
undoable edit
item list
selection
window other
button X   X   X      
check box X   X   X      
color chooser     X          
combo box X       X      
dialog             X  
editor pane   X   X       hyperlink(in the API reference documentation)
file chooser X              
frame             X  
internal frame               internal frame
list           X   list data
menu               menu(in the API reference documentation)
menu item X   X   X     menu key(in the API reference documentation)
menu drag mouse(in the API reference documentation)
option pane                
password field X X   X        
popup menu               popup menu(in the API reference documentation)
progress bar     X          
radio button X   X   X      
slider     X          
tabbed pane     X          
table           X     table model
table column model(in the API reference documentation)
cell editor(in the API reference documentation)
text area   X   X        
text field X X   X        
text pane   X   X         hyperlink(in the API reference documentation)
toggle button X   X   X      
tree                 tree expansion
tree will expand
tree model
tree selection
viewport
(used by scrollpane)
    X          


Previous Page Lesson Contents Next Page Start of Tutorial > Start of Trail > Start of Lesson Search
Feedback Form

Copyright 1995-2001 Sun Microsystems, Inc. All rights reserved.