Java Events 

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 -- in this case, a continuation of the task begun in the lesson on Components.. 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 subsequent lessons.

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

The task for this lesson is to continue building a basic user interface that appears in a separate window, rather than the browser's window. It was begun in the earlier lesson on Components.  There, we built the interface, per se. Here, function is added to process events generated by a user interacting with menus and scrollbars.

Background

To establish a context for this discussion, review the code for producing the visual appearance for a basic application window.

Step 4

In this step, we will process events generated when a user selects a particular menu item.

Step 5

In step 5, events generated when a user moves or clicks on a scrollbar will be processed.

Step 6

In step 5, you could not see the effects of moving a scrollbar. Here, we add a simple drawn object to the display so that those effects can be seen.

Complete Applet

We can now put all the pieces together. The complete program is shown, including a run applet anchor.


Comment

You should now be able to create a basic Java program interface. You just have to add the code to do something useful. In subsequent discussions, we will consider additional user interface components and their layout on the display.