The JDK 1.0 Event Model
In JDK 1.0.2, the default handleEvent method calls other
methods depending on the type of event that occurred:
- action() (Event.ACTION_EVENT) (occurs when a button
is clicked, a menu or list item selected, a checkbox or radio button
is checked, or a textfield completed).
- mouseEnter() (Event.MOUSE_ENTER) (when the mouse
is moved into the component's area).
- mouseExit() (Event.MOUSE_EXIT) (when the mouse
pointer leaves the component).
- mouseMove() (Event.MOUSE_MOVE) (when the mouse is moved).
- mouseDown() (Event.MOUSE_DOWN) (when the mouse button is pressed).
- mouseDrag() (Event.MOUSE_DRAG) (when the mouse is moved with the button pressed).
- mouseUp() (Event.MOUSE_UP) (when the mouse button is released).
- keyDown() (Event.KEY_PRESS or Event.KEY_ACTION) (when a key is pressed).
- keyUp() (Event.KEY_RELEASE or Event.KEY_ACTION_RELEASE) (when a key is released).
- gotFocus() (Event.GOT_FOCUS) (when the component receives keyboard focus).
- lostFocus() (Event.LOST_FOCUS) (when the component loses keyboard focus).
The strange part about AWT is that AWT components ignore events.
A button for instance ignores the action event and passes it to its
container.
That container has to figure out which component was the target of the event.
Below is an example of a small applet with two buttons, and the event
handling code:
public class Button1Applet extends Applet {
public void init() {
add(new Button("Red"));
add(new Button("Blue"));
}
public boolean action(Event evt, Object whatAction) {
if (!(evt.target instanceof Button))
return false;
String buttonLabel = (String)whatAction;
if (buttonLabel == "Red")
setBackground(Color.red);
else if (buttonLabel == "Blue")
setBackground(Color.blue);
repaint();
return true;
}
}