Java Reference
In-Depth Information
16.23
How do you create a timer? How do you start a timer? How do you stop a timer?
✓
✓
Check
16.24
Does the
Timer
class have a no-arg constructor? Can you add multiple listeners to a
timer?
Point
K
EY
T
ERMS
anonymous inner class
609
event source object
602
event 602
event-driven programming
event listener object
603
602
inner class 606
listener interface adapter
event handler 604
event-listener interface
620
603
source object
602
event object
602
C
HAPTER
S
UMMARY
1.
The root class of the event classes is
java.util.EventObject
. The subclasses of
EventObject
deal with special types of events, such as action events, window
events, component events, mouse events, and key events. You can identify the source
object of an event by using the
getSource()
instance method in the
EventObject
class. If a component can fire an event, any subclass of the component can fire the
same type of event.
2.
The listener object's class must implement the corresponding
event-listener interface
.
Java provides a listener interface for every event class. The listener interface is usually
named
XListener
for
XEvent
,
with the exception of
MouseMotionListener
. For
example, the corresponding listener interface for
ActionEvent
is
ActionListener
;
each listener for
ActionEvent
should implement the
ActionListener
interface.
The listener interface contains the method(s), known as the
handler(s)
, which process
the events.
3.
The listener object must be registered by the
source object
. Registration methods
depend on the event type. For
ActionEvent
, the method is
addActionListener
.
In general, the method is named
addXListener
for
XEvent
.
4.
An
inner class
, or
nested class
, is defined within the scope of another class. An inner
class can reference the data and methods defined in the outer class in which it nests, so
you need not pass the reference of the outer class to the constructor of the inner class.
5.
Listener interface adapters
are support classes that provide default implementations
for all the methods in the listener interface. Java provides listener interface adapters
for every AWT listener interface with multiple handlers. A listener interface adapter
is named
XAdapter
for
XListener
.
6.
A source object may fire several types of events. For each event, the source object
maintains a list of registered listeners and notifies them by invoking the
handler
on
the listener object to process the event.
7.
A
MouseEvent
is fired whenever a mouse is pressed, released, clicked, entered,
exited, moved, or dragged on a component. The mouse-event object captures the
event, such as the number of clicks associated with it or the location (
x
- and
y
-
coordinates) of the mouse point.