Java Events I

Events are a fundamental part of the Java user interface architecture. In order to understand how they are used by the programmer, one must understand how they behave within the Java Virtual Machine and within the AWT.

The discussion includes two parts. In the first, basic concepts associated with events are discussed. In the second, those concepts are applied, step by step, to a cumulative task.

The current version of Java is 1.1, which supersedes version 1.0. One of the major differences between the two is their respective event models. The two are incompatible. However, the JDK compiler for version 1.1 will accept the event model of 1.0 so long as none of the event classes of 1.1 are included. Thus, programs may mix some of the classes found in 1.1 with classes found in1.0, but not event classes.

The discussion here concerns the 1.0 event model.

Concepts

This discussion will focus primarily on the AWT's convenience methods. In a later discussion, events will be considered at a more basic level.

Event model

The key to making a Java program do something lies in events. Events are a special type of Java object that are generated by the system when the user presses a key, moves the mouse, or performs some other similar user interface action.

Event class

Event objects are instances of the Java AWT class, Event. It is an unusual class in that its variables are generally more important than its methods.

handleEvent method

Events usually emerge within a Java program through the handleEvent method included in the AWT Component class.

Convenience methods

In addition to the handleEvent method, the Java AWT also includes several convenience methods for handling particular types of events, such as mouseDown, mouseUp, or when the user clicks on an interface button.

Propagation of events

Events are normally handled in a handleEvent method or in an action method. This is often done by programmers' overriding these default methods with their own versions. To do this, they must understand how events are propagated up the class hierarchy and/or up the contained in hierarchy.


Task

We will continue the discussion of Java events by incrementally building a simple applet that includes, first, a panel area for drawing with the mouse and for typing in text from the keyboard and, second, a button that clears the panel. The applet will be built through four cumulative steps.

Step 1

In this step, we build the display. It consists of a button, a panel in which to nest the button, and a panel in which we will draw and enter text.

Step 2

In step 2, we add function that allows the user to draw on the drawing panel by holding down the mouse button and dragging.

Step 3

In step 3, we add function that enables the clear button.

Step 4

We complete the task by adding function that enables the program to catch keyboard events and to draw them on the drawing panel.


References

References useful for this discussion include the following::