
This is the Title of the Book, eMatter Edition
Copyright © 2007 O’Reilly & Associates, Inc. All rights reserved.
440
|
Chapter 19: The Delegation Event Model
Multiple event types from a single event source
Before we move on to the use of our Randomizer class, it’s worth noting that a single
event source can legitimately define more than one kind of event. For example, in
Java (from whence the delegation event model originates), the all-purpose user inter-
face class, Component, defines two categories of mouse events: one for mouse
motion and one for mouse input (i.e., mouseclicks). To register an object for mouse
motion events in Java, we first ensure that it implements the MouseMotionListener
interface, then we pass it to Component.addMouseMotionListener( ). To register an
object for mouse input events, we first ensure that it implements the MouseListener
interface, then we pass it to Component.addMouseListener( ). The same event source,
Component, manages both types of events, keeping separate track of its event listen-
ers by using two separate registration routines and two separate listener lists.
When an event source defines multiple types of events, its listeners can flexibly sign
up to receive only a particular category of events, ignoring other events that are not
of interest. To receive both events, however, a listener object must register twice
(once for each event).
The NightSky Class