5.4 Handling Events

What are events

User actions in the AWT, like pressing a button, scrolling a bar, entering text in a field, are called events. The JVM keeps track of all events and puts them in a queue. From this queue the events are handled one by one in a first-in-first-out order. We call this part of the JVM the event dispatcher.

Events as objects

The events are represented by Event objects. Instance variables describe the state and origin of the event. For all events the following four are of importance:

Especially for mouse events we have also and for key events

Mouse Buttons

Mouses may have one, two or three buttons. Java gives all buttons the same mouse event type. However, different modifiers are set:

Left Button none
Middle Button Event.ALT_MASK
Right Button Event.META_MASK

So this enables to develop programs that offer more functionality on particular platforms.

Event Types

The possible event types can be listed according to the input manner.

But the most important event is a more abstract one:

It is the event that is generated when there is a natural choice for an event. For instance, pressing a button, or entering RETURN in a text field, or selecting an item of a checkbox, a "choice", or a list all are action events.

Handling events

Event Propagation

You, as a programmer, should tell in the java code what exactly should happen when an event is handled. You have a choice where you can put your code.

The Java 1.0 event system offers event propagation: an event can be handled by the component, say a button, at which the event was fired, or by a larger component containing the button. In fact the event can float upwards in the component hierarchy, until it reaches the top component. At every level, the component may intercept the event and take care of actions belonging to the event. After handling the event, it may pass it on to the next larger component, or block further propagation.

When you decide to handle an event at the level of some component, you have to subclass the component to override the standard event handling methods which do nothing. So if you have some buttons in an applet, you can either subclass all the buttons, or write an event handler at the applet level, that contains a large if-statement distinguishing between the buttons with help of the event target.

Event handler methods

The generic event handler method is

	public boolean handleEvent (Event evt) { ... }

The return value is either false, which means that the event is propagated further, or true, which means that propagation is blocked.

There are convenience methods that offer not more functionality, but that allow a cleaner code. For instance,

	public boolean action (Event evt, Object arg) { ... }
can be used to cover all action events. And
	public boolean mouseUp (Event evt, int x, int y) { ... }
      

is for the MOUSE_UP event type. The other convenience methods are mouseDown, mouseMove, mouseDrag, mouseExit, mouseEnter, keyDown, keyUp, lostFocus and gotFocus. If you happen to implement both handleEvent and a convenience method for a particular event, the handleEvent takes precedence.

In the example applet below we give a prototype of mouse event and button event handling.

Making your own events

You can make you own events and with your own event type. Just do the following steps.