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: Swing Features and Concepts

Event Handling

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.

Event source with multiple listeners

Caption: Multiple listeners
can register to be notified of events 
of a particular type from a particular source.
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 Components(in the Creating a GUI with JFC/Swing trail) 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)(in the Creating a GUI with JFC/Swing trail), for instance, you'll find an example of writing a window listener that exits the application when the frame closes.

How to Implement an Event Handler

Every event handler requires three bits of code:
  1. 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 {
         

  2. Code that registers an instance of the event handler class as a listener upon one or more components. For example:
    someComponent.addActionListener(instanceOfMyClass);
         

  3. 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 the ActionListener interface. The program must register this object as an action listener on the button (the event source), using the addActionListener method. When the user clicks the on-screen button, the button fires an action event. This results in the invocation of the action listener's actionPerformed method (the only method in the ActionListener interface). The single argument to the method is an ActionEvent object that gives information about the event and its source.

action listener

                          
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 Using Adapters and Inner Classes to Handle Events(in the Creating a GUI with JFC/Swing trail).

For more information about implementing event handlers, see Writing Event Listeners(in the Creating a GUI with JFC/Swing trail).

Threads and Event Handling

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, the actionPerformed method in the preceding example executes in the event-dispatching thread. Painting code also executes in the event-dispatching thread. This means that while the actionPerformed 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(in the Creating a GUI with JFC/Swing trail).

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.