Java Reference
In-Depth Information
Example 15−2: Clock.java (continued)
/**
* The browser calls this method to tell the applet that it is not visible
* and should not run. It sets a flag that tells the run() method to exit
**/
public void stop() { running = false; }
/**
* Returns information about the applet for display by the applet viewer
**/
public String getAppletInfo() {
return "Clock applet Copyright (c) 2000 by David Flanagan";
}
}
Applets and the Java 1.0 Event Model
The AWT event model changed dramatically between Java 1.0 and 1.1. Chapter 10,
Graphical User Interfaces
described the Java 1.1 event handling model exclusively,
since the Java 1.0 event model is now deprecated. However, because there is still
a large installed base of web browsers (including Netscape 3.0 and 4.0) that sup-
port only the Java 1.0 event model, applets are sometimes still written using this
model. This section briefly describes Java 1.0 event handling and includes an
example applet that uses the model.
*
In Java 1.0, all events are represented by the
java.awt.Event
class. This class has
a number of instance fields that describe the event. One of these fields,
id
, speci-
fies the type of the event.
Event
defines a number of constants that are the possi-
ble values for the
id
field. The
target
field specifies the object (typically a
Component
) that generated the event, or on which the event occurred (i.e., the
source of the event). The other fields may or may not be used, depending on the
type of the event. For example, the
x
and
y
fields are defined when
id
is
BUT-
TON_EVENT
but not when it is
ACTION_EVENT
. The
arg
field can provide additional
type-dependent data.
Java 1.0 events are dispatched first to the
handleEvent()
method of the
Component
on which they occurred. The default implementation of this method checks the
id
field of the
Event
object and dispatches the most commonly used types of events
to various type-specific methods, listed in Table 15-1.
Table 15−1. Java 1.0 Event Processing Methods of Component
action() lostFocus() mouseExit()
gotFocus() mouseDown() mouseMove()
keyDown() mouseDrag() mouseUp()
keyUp()
mouseEnter()
The methods listed in Table 15-1 are defined by the
Component
class. One of the
primary characteristics of the Java 1.0 event model is that you must override these
*
Note that this section and its example are excerpted from
Java Foundation Classes in a Nutshell
.