Java Reference
In-Depth Information
Event Listeners
If a class wants to respond to a user event under the Java event-handling system, it must
implement the interface that deals with the events. These interfaces are called event lis-
teners .
Each listener handles a specific kind of event.
The java.awt.event package contains all the basic event listeners, as well as the objects
that represent specific events. These listener interfaces are the most useful:
ActionListener Action events , which are generated by a user taking an action on
a component, such as a click on a button
n
AdjustmentListener Adjustment events , which are generated when a component
is adjusted, such as when a scrollbar is moved
n
FocusListener Keyboard focus events , which are generated when a component
such as a text field gains or loses the focus
n
ItemListener Item events , which are generated when an item such as a check
box is changed
n
KeyListener Keyboard events , which occur when a user enters text on the key-
board
n
MouseListener Mouse events , which are generated by mouse clicks, a mouse
entering a component's area, and a mouse leaving a component's area
n
MouseMotionListener Mouse movement events , which track all movement by a
mouse over a component
n
WindowListener Window events , which are generated by a window being maxi-
mized, minimized, moved, or closed
n
A class can implement as many listeners as needed. The following class is declared to
handle both action and text events:
public class Suspense extends JFrame implements ActionListener,
TextListener {
// ...
}
To use these classes in your programs, you can import them individually or use an
import statement with a wildcard to make the entire package available:
import java.awt.event.*;
 
Search WWH ::




Custom Search