![]() |
Events and Listeners
|
---|
Every event involves two objects: an event and a listener.
The event object stores information about what happened.
The listener object does something about it.
Normally the event object is created by the system and the
programmer creates the listener object.
The event object carries information that describes the event in more
detail.
Each visual control communicates user input by triggering an event.
|
These are the event / listener pairs that are implemented by
the Abstract Window Toolkit (AWT).
|
The applet in this example listens for
any Action events generated by a button.
|
Figure 1: EventExample1 (execution) |
The Action event is wired in two steps:
Each time the button is pressed, an action event is generated and the actionPerformed() method in the applet is called. The example above implements the ActionListener interface which
consists of the method actionPerformed(). To register itself
as a listener, it creates a button and calls the button's
addActionListener() method. Each time the button is pressed,
a message is written to standard output.
|
This applet listens for events generated by two different buttons.
|
Figure 2: EventExample2 (execution) |
After the objects are linked so that events can flow, the rest of the task is to interrogate the event object. When multiple buttons send action events to the same applet, there must be some way to distinguish which button sent the event. The source of the event is available by sending the getSource() method to the event. The source object is returned and can be compared with each of the possible button source objects. In the method actionPerformed() in the example above, the
event source is compared with the b1 and b2 button
handles to identify which button was pressed.
|
This applet listens for different kinds of events generated by
two buttons.
|
Figure 3: EventExample3 (execution) |
Take a look at the output of this example. Each time an event occurs, the contents of the event are written to the standard output device. Notice that some of the fields like src and id are available in each event. But some fields like x and y are only available on some of the events. An event is a class. It inherits fields from its super class. All the AWT events descend from the AWTEvent class which contains the id field. Continuing up the chain, the AWTEvent class descends from the EventObject class which contains the src field.
Each AWT event contains the fields that are necessary to represent what occurred in the source object. To process an event, one should check the event's API documentation and learn what fields and accessing methods are available and fetch whatever is needed. Note: In some cases, the field names above have been abbreviated (e.g. src is really source). Check Sun's API documentation for the exact field names.
|
The action event / listener pair is one of many
event / listener pairs
that are sent by AWT objects.
The object that sends the event does not need to be a button. It can be a checkbox, textfield, or other object that will allows the registry of a listener. The visual classes that trigger the most events, descend from the Component class in the Java hierarchy. Here are some general steps to follow to construct a class that listens for events:
|
by Noel Enete . . . www.enete.com . . . noel@enete.com |