Java Reference
In-Depth Information
method that is invoked depends on the type of event; different event listener types
define different methods.
All event listener types are interfaces that extend the
java.util.EventListener
interface. This is a marker interface; it does not define any methods of its own, but
exists so that you can use the
instanceof
operator to distinguish event listeners
from other object types. The
java.awt.event
,
javax.swing.event
, and
java.beans
packages define a number of event-listener interfaces that extend the generic
EventListener
interface. Each listener is specific to a particular type of event and
defines one or more methods that are invoked in particular circumstances (e.g.,
java.awt.MouseListener
). The only special feature of an event-listener method is
that it always takes an event object as its single argument.
Note that the
java.awt.event
and
javax.swing.event
packages define only event
listener
inter faces
, not event listener
classes
. A GUI must define its own custom
implementation of an event listener, as it is this implementation that provides the
actual Java code executed in response to an event. These packages do define
some Java classes however: event adapter classes. An
event adapter
is an imple-
mentation of an event listener interface that consists entirely of empty methods.
Many listener interfaces define multiple methods, but often you are interested in
using only one of these methods at a time. In this case, it is easier to subclass an
adapter, overriding only the method you are interested in, instead of implementing
the interface directly and having to define all its methods.
Every AWT and Swing application has an automatically created thread, called the
event dispatch thread
, that invokes the methods of event listeners. When an appli-
cation starts, the main thread builds the GUI, and then it often exits. From then on,
everything that happens takes place in the event dispatch thread, in response to
the invocation of event listener methods. One important side-effect of this imple-
mentation is that AWT and Swing components are not thread-safe, for performance
reasons. Thus, if you are writing a multithreaded program, you must be careful to
call component methods only from the event dispatch thread. Calling component
methods from other threads may cause intermittent and difficult-to-diagnose bugs.
If you need to perform some kind of animation or repetitive task with Swing, use
a
javax.swing.Timer
instead of using a separate thread. If another thread really
needs to interact with a Swing component, use the
java.awt.EventQueue invoke-
Later()
and
invokeAndWait()
methods.
Chapter 2 of
Java Foundation Classes in a Nutshell
describes the event-handling
API in more detail and also contains tables that list the various types of AWT and
Swing event listeners, their methods, and the components that use those listener
interfaces. The following sections contain some examples that illustrate different
ways of using the Java event-handling API, as well as an example that demonstrate
an API for handling certain types of input events using a lower-level API.
Handling Mouse Events
Example 10-11 is a listing of
ScribblePane1.java
, a simple
JPanel
subclass that
implements the
MouseListener
and a
MouseMotionListener
interfaces in order to
receive mouse-click and mouse-drag events. It responds to these events by draw-
ing lines, allowing the user to “scribble” in the pane. Figure 10-10 shows the