Event
Model (in Java 1.1)
event = change of state (state transition) [on UI component]
event ® action when e do
S
|
|
General Model
JDK 1.1 Event Model
(Delegation-based)
When an user action on a
component c generates an event e, applications A1 and A2 ask the component
object c, representing the user interface component c, to execute S1 and
S2, respectively, i.e., the applications delegate
the responsibility of executing S1 and S2 to the component c.
There are different types of
event objects (event classes in
java.awt.event.*) and associated with each event type there are a fixed set of
methods, defined by an event listener interface, that can be
executed by a component.
|
Event Class |
Listener Interface |
Adapter |
Listener Methods |
|
ActionEvent |
ActionListener |
|
actionPerformed() |
|
AdjustmentEvent |
AdjustmentListener |
|
adustmentValueChanged() |
|
ComponentEvent |
ComponentListener |
ComponentAdapter |
componentHidden() componentMoved() componentResized() componentShown() |
|
ContainerEvent |
ContainerListener |
ContainerAdapter |
componentAdded() componentRemoved() |
|
FocusEvent |
FocusListener |
FocusAdapter |
focusGained() focusLost() |
|
ItemEvent |
ItemListener |
|
itemStateChanged() |
|
KeyEvent |
KeyListener |
KeyAdapter |
keyPressed() keyReleased() keyTyped() |
|
MouseEvent |
MouseListener MouseMotionListener |
MouseAdapter MouseMotionAdapter |
mouseClicked() mouseEntered() mouseExited() mousePressed() mouseReleased() mouseDragged() mouseMoved() |
|
TextEvent |
TextListener |
|
textValueChanged() |
|
WindowEvent |
WindowListener |
WindowAdapter |
windowActivated() windowClosed() windowClosing() windowDeactivated() windowDeiconified() windowIconified() windowOpened() |
Event Types, Listnener
Methods, and Adapters
[Table 7-6, page 151, 2nd
Edition]
Each component (c) keeps a list of Listener objects (i.e., an object that
implements a Listener interface), each of which is owned by an application (A1)
and contains the action (method S1) to be executed when the component receives
a corresponding event (e).
For each event listener
interface with more than one methods, there is an adapter class that implements
an empty body for each method in the interface.
|
|
Event Model (JDK 1.1)
|
Component |
Events Generated |
|
Button |
ActionEvent |
|
Checkbox |
ItemEvent |
|
CheckboxMenuItem |
ItemEvent |
|
Choice |
ItemEvent |
|
Component |
ComponentEvent FocusEvent KeyEvent MouseEvent |
|
Container |
ContainerEvent |
|
List |
ActionEvent ItemEvent |
|
MenuItem |
ActionEvent |
|
Scrollbar |
AdjustmentEvent |
|
TextComponent |
TextEvent |
|
TextField |
ActionEvent |
|
Window |
WindowEvent |
AWT
Components and The Events They Generate [Table 7-7, page 152]
WindowEvent e
InOut
Example 3
|
WindowListener
Object (Implements
WindowListner Interface) |
||||||||||||
![Text Box: import java.awt.*;
import java.awt.event.*;
class InOut1 {
public static void main(String[] args) {
Frame f = new Frame("InOut");
f.resize(200,100);
f.show();
f.addWindowListener(
new WindowAdapter(){
public void windowClosing(WindowEvent e) {
System.exit(0);
}
}
);
}
}
/* do nothing, but exit by window close */](./EventModel_files/image005.gif)
Event Classes

MouseEvent methods
Object getSource()
Component getComponent()
long getWhen() time-stamp for the event
int getModifiers() keyboard
modifier, mouse button
int getClickCount()
Point getPoint()
int getX()
int getY()
ActionEvent methods
String getActionCommand()
A string set by
Button or MenuItem method
setActionCommand(String)
InOut
Examples
|
Create a frame of size width (x) = 200 height (y) = 100 Does not terminate, i.e., no response to events: WindowClose windowOpened windowClosing
windowClosed windowIconified windowDeiconified windowActivated
windowDeactivated java.awt = a package (class library)
for Abstract Windowing Toolkit (AWT) java.awt.* = all classes in java.awt import = make the class names
defined java.awt available to the following context |
1.
class
InOut0 {
public static void main(String[] args) {
java.awt.Frame f = new java.awt.Frame("InOut");
f.resize(200,100);
f.show();
}

}
2.
import java.awt.*;
class
InOut0_1 {
public static void main(String[] args) {
Frame f = new
Frame("InOut");
f.resize(200,100);
f.show();
}
}

3.
|
Frame f is a container. When user clicks the window closing button of f, a windowClosing event is generated by f
(i.e., f is the source of the event.) f, as a component, maintains a list of objects that implements the
interface WindowListener. The InOut1 class creates a new object of an anonymous subclass of WindowAdaptor class, and add it to
f’s list of WindowListeners. The anonymous subclass object overriding
the WindowAdaptor’s method, windowClosing(), defines the action
to be performed on behalf of the InOut1 object, i.e., System.exit(). When a windowClosing event
occurs, the frame f executes the windowClosing method of each element of its WindowListener’s list. [JDK1.1 Event Model] |
import
java.awt.*;
import
java.awt.event.*;
class
InOut1 {
public static void main(String[] args) {
Frame f = new
Frame("InOut");
f.resize(200,100);
f.show();
f.addWindowListener(new WindowAdapter(){
public void windowClosing(WindowEvent
e) {
System.exit(0);
}
});
}
}
/* do nothing, but exit by window close */
package
java.awt.event;
import
java.util.EventListener;
public
interface WindowListener extends
EventListener {
public void windowOpened(WindowEvent e);
public void windowClosing(WindowEvent e);
public void windowClosed(WindowEvent e);
public void windowIconified(WindowEvent
e);
public void windowDeiconified(WindowEvent
e);
public void windowActivated(WindowEvent
e);
public void windowDeactivated(WindowEvent
e);
}
package
java.awt.event;
public abstract class WindowAdapter implements WindowListener {
public void windowOpened(WindowEvent e) {}
public void windowClosing(WindowEvent e) {}
public void windowClosed(WindowEvent e) {}
public void windowIconified(WindowEvent e)
{}
public void windowDeiconified(WindowEvent
e) {}
public void windowActivated(WindowEvent e)
{}
public void windowDeactivated(WindowEvent
e) {}
}
import
java.awt.*; import
java.awt.event.*; class
InOut1 { public static void main(String[] args)
{ Frame f = new
Frame("InOut"); f.resize(200,100); f.show(); f.addWindowListener(new WindowAdapter(){ public void windowClosing(WindowEvent e) { System.exit(0); } }); } } /* do nothing, but exit by window close */ WindowListener
Object
InOut
Example 3
