The following schematic illustrates the basic steps in event handling
for a button "click":
The operating system detects the physical signal generated by the
mouse click when the cursor resides over a button. The OS sends
a message with data about the event to the peer component object
in the AWT system of the JVM, which then checks for event listeners
registered to receive events for that type of event occuring over
a particular component.
There are different kinds of listeners for different kinds of events.
A listener implements the interface for a particular kind of event
. Here are some examples:
- Button clicks - ActionListener
- Mouse button clicks - MouseListener
- Mouse movements - MouseMotionListener
- Closing a frame - WindowListener
Note that these interfaces subclass the EventListener
interface.
The button class includes the method addActionListener(ActionListener).
It allows for objects that implement the ActionListener
interface to add themselves to a button's list of objects to call
when it is clicked. (Thus the ActionListener
interface provides for a callback
reference.)
The call is made by invoking the actionPerformed(ActionEvent
ae) method of the ActionListener
interface, which the classes will override with their own code.
The details of event generation are beyond this course but the
following diagram shows roughly the steps taken by the component
over which the event occurred. The DispatchEvent()
and ProcessEvent()
methods see that the registered listeners are called and passed
the AWTEevent
(or its subclass) object.
We see that a button event results in all of the ActionListener
objects that are registered with that button getting their actionPerformed(ActionEvent
ae) methods invoked. The ActionEvent
ae object carries the information about the event such as
what button generated it. The actionPerformed()
method can use this info to identify the source of the event and
decide what to do based on that information.
References and Web Resources
Latest update Nov. 1, 2004
|