Overview of the Java UI |
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:
- by modifying the Event instance before it goes further up the hierarchy -- for example, a TextField subclass that displays all letters in uppercase might react to the keypress of a lowercase letter by changing the Event to contain the uppercase version of the letter
- by reacting in some other way to the event -- for example, a TextField subclass (or a TextField's Container) could react to a Return keypress by calling a method that processes the text field's contents
- by stopping the event from being processed further -- for example, if an invalid character is entered in a text field, an event handler could simply stop the Event from being processed any further
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 object. An Event object includes the following information:
- The type of the event -- for example, a key press or mouse click, or a more abstract event such as an "action" or window iconification.
- The object that was the "target" of the event -- for example, the Button corresponding to the onscreen button the user clicked, or the TextField corresponding to the field that user just typed in.
- A timestamp indicating when the event occurred.
- The location (x,y) where the event occurred.
- The key that was pressed (for keyboard events).
- An arbitrary argument (such as the string displayed on the Component) associated with the Event.
- The state of the modifier keys when the event occurred.
How to Implement an Event Handler
Components can respond to events by implementing thehandleEvent()
method or by implementing a method that's specific to one type of event. The latter works because the default (Component) definition of thehandleEvent()
method simply calls a method that's specific to the event type. These event-specific methods aremouseEnter(), mouseExit(), mouseMove(), mouseUp(), mouseDown(), mouseDrag(), keyDown(),
andaction().
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 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: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 returnspublic 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; }false
so that the event will be fully processed. We would returntrue
if we wanted to stop the event from being processed further.
Overview of the Java UI