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 handleEvent() method to catch events resulting from user actions on the slider (Scrollbar), text field (TextField), and pop-up list (Choice).

Here is the ConversionPanel implementation of the handleEvent() method:

public boolean handleEvent(Event e) {
    if (e.target instanceof Scrollbar) {
        textField.setText(String.valueOf(slider.getValue()));
        controller.convert(this);
    } else if ((e.target instanceof TextField) 
    	       && (e.id == Event.ACTION_EVENT)) {
        setSliderValue(getValue());
        controller.convert(this);
    } else if ((e.target instanceof Choice)
    	       && (e.id == Event.ACTION_EVENT)) {
        controller.convert(this);
    } 
    return false;
}
The method simply makes sure that the ConversionPanel's slider and text field both show the same value, and then asks the Converter object to update the other ConversionPanel. The method always returns false so that the event will be fully processed. We would return true if we wanted to stop the event from being processed further.


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