![]() ![]() ![]() |
Start of Tutorial > Start of Trail > Start of Lesson | Search |
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 ActionListener
User closes a frame (main window) WindowListener
User presses a mouse button while the cursor is over a component MouseListener
User moves the mouse over a component MouseMotionListener
Component becomes visible ComponentListener
Component gets the keyboard focus FocusListener
Table or list selection changes ListSelectionListener
Each 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.
Whenever you want to detect events from a particular component, first check the how-to section for that component. All of the component how-to sections can be found in the Using Swing ComponentsCaption: Multiple listeners can register to be notified of events of a particular type from a particular source.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 theActionListener
interface. The program must register this object as an action listener on the button (the event source), using theaddActionListener
method. When the user clicks the on-screen button, the button fires an action event. This results in the invocation of the action listener'sactionPerformed
method (the only method in theActionListener
interface). The single argument to the method is anActionEvent
object that gives information about the event and its source. Caption: When the user clicks a button, the button's action listeners are notified.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 Getting Started
.
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, theactionPerformed
method in the preceding example executes in the event-dispatching thread. Painting code also executes in the event-dispatching thread. This means that while theactionPerformed
method 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 |