GUI Event Handling
Topics
The Delegation Event Model Event Classes Event Listeners
> > > >
ActionListener Method MouseListener Methods MouseMotionListener Methods WindowListener Methods
Steps for Creating GUI Applications with Event Handling
2
Delegation Event Model
What is Delegation Event Model?
The Delegation Event Model
> Model used by Java to handle user interaction
with GUI components > Describes how your program can respond to user interaction
Three important players
> Event Source > Event Listener/Handler > Event Object
Event Source, Event Listener/Handler
Event Source
> GUI component that generates the event > Example: button
Event Listener/Handler
> Receives and handles events > Contains business logic > Example: displaying information useful to the
user, computing a value
Event Object
Created when an event occurs (i.e., user interacts with a GUI component) Contains all necessary information about the event that has occurred
> Type of event that has occurred > Source of the event
Represented by an Event class
Event Listener Registration to Event Source in Delegation Event Model
Event Listener Registers to Event Source
A listener should be registered with a source Once registered, listener waits until an event occurs When an event occurs
> An event object created by the event source > Event object is fired by the event source to the
registered listeners (method of event listener is called with an event object as a parameter)
Once the listener receives an event object from the source
> Deciphers the event > Processes the event that occurred.
Control Flow of Delegation Event Model
Methods of Event Source Used by Event Listeners for Registation
Event source registering a listener:
void add<Type>Listener(<Type>Listener listenerObj) where, > <Type> depends on the type of event source
> Can be Key, Mouse, Focus, Component, Action and others
> One event source can register several listeners
Registered listener being unregistered:
void remove<Type>Listener(<Type>Listener listenerObj)
10
Event Classes
Event Classes
The EventObject class
> Found in the [Link] package
The AWTEvent class
> > > >
An immediate subclass of EventObject Defined in [Link] package Root of all AWT-based events Subclasses follow this naming convention:
<Type>Event
12
Event Classes
13
Event Listeners
Event Listeners
Classes that implement the <Type>Listener interfaces
15
ActionListener Method
Contains exactly one method
16
MouseListener Methods
17
MouseMotionListener Methods
18
WindowListener Methods
19
Steps for Creating GUI Application with Event Handling
Steps for Creating GUI Applications with Event Handling
[Link] a GUI class
> Describes and displays the appearance of your
GUI application
[Link] Event Listener class (a class implementing the appropriate listener interface)
> Override all methods of the appropriate listener
interface > Describe in each method how you would like the event to be handled > May give empty implementations for methods you don't need
21
Steps for Creating GUI Applications with Event Handling (Continued)
[Link] the listener object with the event source
> The object is an instantiation of the listener class
in step 2 > Use the add<Type>Listener method of the event source
22
Mouse Events Example (page #1)
1 2 3
import [Link].*; import [Link].*; public class MouseEventsDemo extends Frame implements MouseListener, MouseMotionListener { TextField tf; public MouseEventsDemo(String title){ super(title); tf = new TextField(60); // Register event listener to the event source addMouseListener(this); } //continued...
4 5 6 7 8 9 10 11
23
Mouse Events Example (page #2)
11 12 13 14 15 16 17 18 19 20 21 22 23 24
// Displays GUI public void launchFrame() { /* Add components to the frame */ add(tf, [Link]); setSize(300,300); setVisible(true); } // Implement methods of event listener interface public void mouseClicked(MouseEvent me) { String msg = "Mouse clicked."; [Link](msg); } //continued...
24
Mouse Events Example (page #3)
22 23 24 25 26 27 28 29 30 31 32 33 34
public void mouseEntered(MouseEvent me) { String msg = "Mouse entered component."; [Link](msg); } public void mouseExited(MouseEvent me) { String msg = "Mouse exited component."; [Link](msg); } public void mousePressed(MouseEvent me) { String msg = "Mouse pressed."; [Link](msg); } //continued...
25
Mouse Events Example (page #4)
35 36 37 38 39 40 41 42 43 44
public void mouseReleased(MouseEvent me) { String msg = "Mouse released."; [Link](msg); } public void mouseDragged(MouseEvent me) { String msg = "Mouse dragged at " + [Link]() + "," + [Link](); [Link](msg); } //continued...
26
Mouse Events Example (page #5)
45 46 47 48 49 50 51 52 53 54 55 56 57
public void mouseMoved(MouseEvent me) { String msg = "Mouse moved at " + [Link]() + "," + [Link](); [Link](msg); } // Main method public static void main(String args[]) { MouseEventsDemo med = new MouseEventsDemo("Mouse Events Demo"); [Link](); } }
27
Close Window Example (page #1)
1 2 3 4 5 6 7 8 9 10 11 12
import [Link].*; import [Link].*; class CloseFrame extends Frame implements WindowListener { Label label; CloseFrame(String title) { super(title); label = new Label("Close the frame."); [Link](this); } //continued...
28
Close Window Example (page #2)
13 14 15 16 17 18 19 20 21 22 23 24 25 26
void launchFrame() { setSize(300,300); setVisible(true); } // Implement methods of listener interface public void windowActivated(WindowEvent e) { } public void windowClosed(WindowEvent e) { } public void windowClosing(WindowEvent e) { setVisible(false); [Link](0); } //continued...
29
Close Window Example (page #3)
26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41
public } public } public } public }
void windowDeactivated(WindowEvent e) { void windowDeiconified(WindowEvent e) { void windowIconified(WindowEvent e) { void windowOpened(WindowEvent e) {
// Main method public static void main(String args[]) { CloseFrame cf = new CloseFrame("Close Window Example"); [Link](); } }
30
Adaptor Classes
Adapter Classes
Why use Adapter classes?
> Implementing all methods of an interface takes a
lot of work > Interested in implementing some methods of the interface only
Adapter classes
> Built-in in Java > Implement all methods of each listener interface
with more than one method > Implementations of the methods are all empty
32
Adapter Classes: Close Window Example
1 2 3 4 5 6 7 8 9 10 11 12 13
import [Link].*; import [Link].*; class CloseFrame extends Frame{ Label label; CFListener w = new CFListener(this); CloseFrame(String title) { super(title); label = new Label("Close the frame."); [Link](w); } //continued...
33
Adapter Classes: Close Window Example
14 15 16 17 18 19 20 21 22 23 24 25
void launchFrame() { setSize(300,300); setVisible(true); } public static void main(String args[]) { CloseFrame cf = new CloseFrame("Close Window Example"); [Link](); } } //continued...
34
Adapter Classes: Close Window Example
25 26 27 28 29 30 31 32 33 34 35
class CFListener extends WindowAdapter { CloseFrame ref; CFListener( CloseFrame ref ){ [Link] = ref; } public void windowClosing(WindowEvent e) { [Link](); [Link](1); } }
35
Inner Classes
Class declared within another class Why use inner classes?
> Help simplify your programs > Especially in event handling
36
Inner Classes: Close Window Example
1 2 3 4 5 6 7 8 9 10 11 12
import [Link].*; import [Link].*; class CloseFrame extends Frame{ Label label; CloseFrame(String title) { super(title); label = new Label("Close the frame."); [Link](new CFListener()); } //continued...
37
Inner Classes: Close Window Example
13 14 15 16 17 18 19 20 21 22 23 24
void launchFrame() { setSize(300,300); setVisible(true); } class CFListener extends WindowAdapter { public void windowClosing(WindowEvent e) { dispose(); [Link](1); } } //continued...
38
Inner Classes: Close Window Example
25 26 27 28 29 30
public static void main(String args[]) { CloseFrame cf = new CloseFrame("Close Window [Link](); } } Example");
39
Anonymous Inner Classes
Unnamed inner classes Why use anonymous inner classes?
> Further simplify your codes > Especially in event handling
40
Anonymous Inner Classes: Close Window Example
1 2 3 4 5 6 7 8 9 10 11 12 13
import [Link].*; Label label;
import [Link].*;
class CloseFrame extends Frame{ CloseFrame(String title) { super(title); label = new Label("Close the frame."); [Link](new WindowAdapter(){ public void windowClosing(WindowEvent e){ dispose(); [Link](1); } }); }
41
Anonymous Inner Classes: Close Window Example
14 15 16 17 18 19 20 21 22 23 24
void launchFrame() { setSize(300,300); setVisible(true); } public static void main(String args[]) { CloseFrame cf = new CloseFrame("Close Window Example"); [Link](); } }
42
Thank You!