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.
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:
id
, the event type, like a button press, a mouse action;target
, which refers the component (button, etc.) where
the event comes from;when
, a time-stamp.arg
, any object as argument (for a textfield it is the entered text,
for a scroll bar it is the number scrolled to).x
and y
, the coordinates of the pointer, andclickCount
for double-clickskey
the key itselfmodifiers
like an ALT, SHIFT, or CONTROL - key.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.
The possible event types can be listed according to the input manner.
But the most important event is a more abstract one:
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.
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.
new
;deliverEvent()
.