|      | Start of Tutorial > Start of Trail > Start of Lesson | Search Feedback Form | 
 
Every time the user types a character or pushes a mouse button, an event occurs. Any object can be notified of the event. All it has to do is implement the appropriate interface and be registered as an event listener on the appropriate event source. Swing components can generate many kinds of events. Here are a few examples:
Act that results in the event Listener type User clicks a button, presses Return while typing in a text field, or chooses a menu item ActionListenerUser closes a frame (main window) WindowListenerUser presses a mouse button while the cursor is over a component MouseListenerUser moves the mouse over a component MouseMotionListenerComponent becomes visible ComponentListenerComponent gets the keyboard focus FocusListenerTable or list selection changes ListSelectionListenerEach event is represented by an object that gives information about the event and identifies the event source. Event sources are typically components, but other kinds of objects can also be event sources. As the following figure shows, each event source can have multiple listeners registered on it. Conversely, a single listener can register with multiple event sources.
lesson. The how-to sections give examples of handling the events that you're most likely to care about. In How to Make Frames (Main Windows)
, for instance, you'll find an example of writing a window listener that exits the application when the frame closes.
Every event handler requires three bits of code:
- In the declaration for the event handler class, code that specifies that the class either implements a listener interface or extends a class that implements a listener interface. For example:
public class MyClass implements ActionListener {
- Code that registers an instance of the event handler class as a listener upon one or more components. For example:
someComponent.addActionListener(instanceOfMyClass);
- Code that implements the methods in the listener interface. For example:
public void actionPerformed(ActionEvent e) { ...//code that reacts to the action... }Let's investigate a typical event-handling scenario by looking at how buttons (
JButton) handle mouse clicks. To detect when the user clicks an on-screen button (or does the keyboard equivalent), a program must have an object that implements theActionListenerinterface. The program must register this object as an action listener on the button (the event source), using theaddActionListenermethod. When the user clicks the on-screen button, the button fires an action event. This results in the invocation of the action listener'sactionPerformedmethod (the only method in theActionListenerinterface). The single argument to the method is anActionEventobject that gives information about the event and its source.Event handlers can be instances of any class. Often, an event handler that has only a few lines of code is implemented using an anonymous inner class -- an unnamed class defined inside of another class. Anonymous inner classes can be somewhat confusing at first, but once you're used to them they make code clearer by keeping the implementation of an event handler close to where the event handler is registered. For information about using inner classes, see Using Adapters and Inner Classes to Handle Events
.
For more information about implementing event handlers, see Writing Event Listeners
.
Event-handling code executes in a single thread, the event-dispatching thread. This ensures that each event handler will finish executing before the next one executes. For instance, theactionPerformedmethod in the preceding example executes in the event-dispatching thread. Painting code also executes in the event-dispatching thread. This means that while theactionPerformedmethod is executing, the program's GUI is frozen -- it won't repaint or respond to mouse clicks, for example.
Important: The code in event handlers should execute very quickly! Otherwise, your program's perceived performance will be poor. If you need to perform some lengthy operation as the result of an event, do it by starting up another thread (or somehow sending a request to another thread) to perform the operation. For help on using threads, see How to Use Threads.
 
|      | Start of Tutorial > Start of Trail > Start of Lesson | Search Feedback Form | 
Copyright 1995-2002 Sun Microsystems, Inc. All rights reserved.