Noel Enete
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.

Event / Listener Pairs


These are the event / listener pairs that are implemented by the Abstract Window Toolkit (AWT).
ActionEvent
AdjustmentEvent
ComponentEvent
ContainerEvent
FocusEvent
ItemEvent
KeyEvent
MouseEvent
MouseMotionEvent
TextEvent
WindowEvent
ActionListener
AdjustmentListener
ComponentListener
ContainerListener
FocusListener
ItemListener
KeyListener
MouseListener
MouseMotionListener
TextListener
WindowListener

Simple Example


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:

  1. Make the applet class an ActionListener. Since ActionListener is an interface and it only specifies the actionPerformed() method, an actionPerformed() method that receives an ActionEvent object is coded inside the applet.
  2. Select a visual object that supports action events (e.g. a button). Register the applet with the button as a listener. By using addActionListener(), the button learns that the applet wants to know about action events.

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.

Events from Multiple Components Example


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.

Multiple Events Example


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.

EventObject
src
|
AWTEvent
id
|
FocusEvent
tmp?
parm




MouseEvent
cnt
pt
x
y
pop
parm
ActionEvent
cmd
modf
parm



When the example program prints the contents of an event to the standard output device, it identifies the class from which each field is drawn.

EventObject: src =
AWTEvent: id =
MouseEvent: cnt =
MouseEvent: pt =
MouseEvent: x =
MouseEvent: y =
MouseEvent: pop =
MouseEvent: parm =

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.

How Do I Find and Use Events?


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:

  1. Select a visual class that descends from the Component class.
  2. Check to see what listener registration methods are included in that class (or in its super classes). These are methods that have the form addXxxListener(). Select one of them.
  3. Find the documentation on the corresponding listener and identify what methods must be coded. Then code those methods into the target applet or class. Also, code the implements phrase into the class definition.
  4. Create the selected visual object and call its addXxxListener() method and pass this object as a parameter.
  5. Take a look at the event class (and its super classes) to see what kind of information is available and tap into whatever looks userful.



by Noel Enete . . . www.enete.com . . . noel@enete.com