Event Handling
Used by the Abstract Window Toolkit (AWT) for basic GUI
programming Java 1.0
Used by Swing -Better components than AWT Java 2
Used by JavaBeans -- reusable software components, like
Visual Basic, that can be manipulated
in a builder tool
Babysitting
A baby in the home needs attention.
Many events surrounding the baby are not easily ignored.
Events can be of different types.
Babysitting
Before responding to an event, a babysitter typically takes note
of the source and the type of event.
Babysitting
Handler
Event type
Event source
The sitter needs to know both the
event type and the event source.
Event Handling
The window manager software may generate hundreds of different
events.
Examples include mouse clicks, mouse movements, key strokes,
and timer ticks.
A programmer is typically interested in a small subset of all of the
possible events that may occur.
Object
EventObject
AWTEvent
ActionEvent
ComponentEvent
String getActionCommand()
InputEvent
boolean isAltDown()
MouseEvent
WindowEvent
Window getWindow()
KeyEvent
int getX()
char getKeyChar()
10
AAmouse
mouse
object
object
Event Source
Example
Event data and methods
An
Anevent
eventobject
object
that
thatdescribes,
describes,
say,
say,the
thexxand
andyy
coordinate
coordinateof
of
where
wherethe
themouse
mouse
was
wasclicked
clicked
Listener
The
Thelistener
listenerobject
object
has
hasmethods
methodsthat
that
are
arecalled
calledfor
for
particular
particularevents
events
11
Example
AAmouse
mouse
object
object
An
Anevent
eventobject
object
that
thatdescribes,
describes,
say,
say,the
thexxand
andyy
coordinate
coordinateof
of
where
wherethe
themouse
mouse
was
wasclicked
clicked
Implements
MouseListener
The
Thelistener
listenerobject
object
has
hasmethods
methodsthat
that
are
arecalled
calledfor
for
particular
particularevents
events
12
The Listener
There are different types of Listeners.
MouseListeners
WindowListeners
ScrollBarListeners
Etc..
These Listeners are interfaces. Remember what an interface
provides? If class X implements an interface then class X promises
to provide (at least) the methods declared in the interface.
95-712 OOP Java
13
ActionListener
abstract void actionPerformed(ActionEvent e);
MouseListener
KeyListener
14
A Listener Interface
public interface MouseListener {
void mouseClicked(MouseEvent e);
void mouseEntered(MouseEvent e);
void mouseExited(MouseEvent e);
void mouseReleased(MouseEvent e);
}
//What does it mean if I claim that I implement this interface?
15
An Example
// MouseSpyApplet.java
import java.applet.Applet;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
16
I have to provide
ALL of these methods!!
17
18
19
Another approach
Suppose a friendly sole created this class:
public class MouseAdapter implements MouseListener {
void mouseClicked(MouseEvent e){}
void mouseEntered(MouseEvent e){}
void mouseExited(MouseEvent e){}
void mousePressed(MouseEvent event) {}
void mouseReleased(MouseEvent e){}
}
Now, suppose I extend this class. What must I provide?
95-712 OOP Java
20
21
Inner Classes
22
23
//NestedTopLevelExample.java
class Top {
int i,j;
static class SomeClass { // static makes it top-level nested
int k;
SomeClass() {
System.out.println("Constructing SomeClass");
}
void foo() { System.out.println("Hello"); }
}
Top() {
System.out.println("Constructing a Top object");
}
}
24
Output
Constructing a Top object
Constructing SomeClass
Hello
95-712 OOP Java
25
26
27
// MemberClassExample.java
class Top {
int i = 33;
public class SomeClass {
// access the outer object's state.
private int k = i;
SomeClass() {
System.out.println("Constructing SomeClass");
}
void foo() { System.out.println("Hello"); }
}
Top() {
System.out.println("Constructing a Top object");
SomeClass sc = new SomeClass();
System.out.println(sc.k);
}
95-712 OOP Java
28
}
29
Local Classes
A local class is an inner class. Typically, a local class
is declared within a method. It is not a member of an
enclosing class. It is visible only within a block.
These classes are used primarily as "adapter classes".
For example, a block of code that creates a Button object
could use a local class to define a simple implementation
of the ActionListener Interface. Then it could instantiate
this simple implementation and pass the resulting object
to the button's ActionListener method, thereby connecting
the button to the "callback" code that is executed when
the button is pressed.
95-712 OOP Java
30
32
Anonymous Classes
An anonymous class is refinement of inner classes.
It allows you to combine the definition of the class
with the instance allocation.
Since it is instantiated in the same expression that defines
it, it can only be instantiated once. This is very similar to
local classes.
When writing a simple adapter class, the choice between
a named local class and an unnamed anonymous class
typically comes down to a matter of style and code clarity,
rather than any difference in functionality.
The new class can't have a constructor.
33
// Anonymous.java
interface SmallClass {
public void foo();
}
class Top {
int i = 33;
void someMethod(SmallClass s) {
s.foo();
}
void anotherMethod() {
someMethod(new SmallClass() {
public void foo() {
System.out.println("Really fun");
}
});
95-712 OOP Java
}
34
Top() {
System.out.println("Constructing a Top object");
someMethod(new SmallClass() {
public void foo() {
System.out.println("Strange but fun");
}
});
}
}
35
36
fire events
Listener Object
Event object
37
Event Handling
Suppose we have a Button object
Button b
ActionEvent
ActionEvent
Object
Object
95-712 OOP Java
38
Implementing a Listener
We need an object that will listen for the ActionEvent.
We implement the ActionListener class (this class listens
for ActionEvents from buttons, menus, etc.) and override
its actionPerformed method.
39
sitter
40
41
addActionListener
Button Object
Sitter Object
42
43
45
ComponentAdapter
MouseMotionAdapter
WidowAdapter
ContainerAdapter
MouseAdapter
FocusAdapter
KeyAdapter
95-712 OOP Java
46
47