Prev Up Next
Go backward to Drawing Methods and the "pen"
Go up to Top
Go forward to Interface Components

Methods for Handling Events

In addition to providing primitives for drawing, JavaTools includes mechanisms to facilitate the construction of applets that react to user input from the mouse or keyboard. The basis of these mechanisms is the notion of event handling methods. As described in detail below, JavaTools defines a set of distinguished method names that are associated with user input events. If the programmer of an applet defines a method with one of these distinguished names, then the browser, through JavaTools, will invoke this method whenever the associated user input event occurs.

For example, "mouseDown" is one of these distinguished names. If a method with this name is defined in an applet, then this method will be invoked whenever the user depresses the button on the mouse. If no such method is defined, then no special action will be taken when the mouse button is depressed.

Flexibility is provided for the handling of user events by allowing the programmer to obtain additional information about the event that occurred through parameters passed to the event handling methods. The "mouseDown" method, for example, will be passed the screen coordinates of the mouse at the time the button was depressed if it is defined as:


void mouseDown( int xcoord, int ycoord) { ... }
The body of the method can then refer to these values through the parameter names. (The names "xcoord" and "ycoord" used in this example can be replaced by any other appropriate names for the parameters. The only essential thing is that the method be defined to expect two integer parameters.)

To allow for the varying amounts of information needed to handle user input events in different applets, many of the event handling methods come in several varieties that differ in what parameters are expected. "mouseDown", for example, can either be defined to expect two parameters as shown above or defined as:


void mouseDown( ) { ... }
in which case it will simply be invoked to inform the applet that the mouse button has been depressed without including any information about where the mouse was positioned at the time.

While you may choose whichever of these two forms of "mouseDown" is most appropriate for your applet, no other variations are allowed. For example, if you wrote an applet that only needed to know the x coordinate of the mouse's positions when the button was depressed, you would still need to define "mouseDown" to expect two integer parameters as in:

void mouseDown( int xcoord, int notused) { ... }
If you instead defined "mouseDown" as
void mouseDown(int xcoord ) { ... }
to indicate only one parameter was of interest, your "mouseDown" method would never be invoked. JavaTools only recognizes the variations of parameterization of event handling methods described in this document. Any other variations will cause the system to fail to recognize your method definition as an event handling method.

In cases where multiple versions of an event handling method with different parameter expectations are recognized by JavaTools, only one version should be included in any applet.

With this background provided, the rest of this section will be devoted to descriptions of those event handling methods that are independent of any particular user interface component (i.e. button, or scrollbar, or...). As in our presentation of drawing methods, each of these sections will begin with a list of "templates" showing the variations of parameterization possible with the method described.

  • The begin Method
  • The mouseExit Method
  • The mouseEnter Method
  • The mouseDown Method
  • The mouseUp Method
  • The mouseMove Method
  • The mouseDrag Method

  • Prev Up Next