Previous | Next | Trail Map | Creating a User Interface | Overview of the Java UI


Event Handling

When the user acts on a Component -- clicking it or pressing the Return key, for example -- an Event object is created. The AWT event-handling system passes the Event up the Component hierarchy, giving each Component a chance to react to the event before the window system fully processes it. Each Component's event handler can either ignore an event or react to it in any of the following ways:

From a Component's view, the AWT event-handling system is more like an event-filtering system. Window-system-dependent code generates an event, but Components get a chance to modify, react to, or destroy the event before the window-system-dependent code fully processes the event. [CHECK -- is this the best, most correct way of saying this?] The following [NON-EXISTENT] figure shows the chain of event handling for a TextField event in the example program.

Note: In the current release, mouse events are forwarded to Components after the window-system-dependent code has fully processed the event. So although Components can intercept all keyboard events, they can't currently intercept mouse events.

The Event Object

Each event results in the creation of an Event(in the API reference documentation)object. An Event object includes the following information:

How to Implement an Event Handler

Components can respond to events by implementing the handleEvent() method or by implementing a method that's specific to one type of event. The latter works because the default (Component) definition of the handleEvent() method simply calls a method that's specific to the event type. These event-specific methods are mouseEnter(), mouseExit(), mouseMove(), mouseUp(), mouseDown(), mouseDrag(), keyDown(), and action().

Important: All event handler methods must execute quickly! Otherwise, they'll destroy the perceived performance of your program. If you need to perform some lengthy operation as the result of an event, do it by starting up another thread (or sending a request to another thread) to perform the operation. For help on using threads, see Threads of Control(in the Writing Java Programs trail)

In the example program, all the event handling is performed by ConversionPanels. They use the action() method to catch events resulting from user actions on the text field (TextField), and pop-up list (Choice). To catch events resulting from user actions on the slider (Scrollbar), they use the handleEvent() method.

Here is the ConversionPanel implementation of the action() and handleEvent() methods:

/** Respond to user actions on controls. */
public boolean action(Event e, Object arg) {
    if (e.target instanceof TextField) {
        setSliderValue(getValue());
        controller.convert(this);
        return true;
    }
    if (e.target instanceof Choice) {
        controller.convert(this);
        return true;
    } 
    return false;
}

/** Respond to the slider. */
public boolean handleEvent(Event e) {
    if (e.target instanceof Scrollbar) {
        textField.setText(String.valueOf(slider.getValue()));
        controller.convert(this);
    } 
    return super.handleEvent(e);
}
The methods simply make sure that the ConversionPanel's slider and text field both show the same value, and then ask the Converter object to update the other ConversionPanel. The action() method returns true if it handled the event; otherwise, it returns false so its higher ups in the component hierarchy can have a look at the object. The handleEvent() method always returns super.handleEvent() so that every event will be fully processed.

Note: If handleEvent() returned true or false (instead of calling its superclass's implementation), the action() method would never be called. Risks like this are part of the reason why we advise you to avoid implementing handleEvent() unless it's absolutely necessary.


Previous | Next | Trail Map | Creating a User Interface | Overview of the Java UI