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 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 their respective event classes. The discussion here concerns the 1.1 event model. A similar discussion is available for the version 1.0 event model.

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. Only the basics of Java events will be introduced here, and the discussion will be limited to just a few of the different types of events that are available. Additional details and features will be discussed in the Java Events II lesson. Java beans also generate events, but they will not be considered here.

Concepts

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 classes

Event objects are instances of one of the subclasses that derive from the Java AWTEvent class.

 

Registration

Registration is the process whereby one object can ask another object (or itself) to notify it when it receives a particular kind of event.

Forwarding

Events are sent, first, to the Component in which they are generated. However, they are normally forwarded for processing to other objects that have registered with the receiving object.

Receiving

Classes that wish to receive Events must implement the Interfaces associated with those Events. They may then process event objects within the methods included in the interfaces.


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, second, a button that clears the panel. It is the same applet that is included in the On-Line Tools list, accessible from the left panel.

The applet will be built in three 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 complete the task by adding function that enables the clear button.


References

References useful for this discussion include the following::