CS 102
Object Oriented Programming
GUI Event Handling
Sources:
http://www3.ntu.edu.sg/home/ehchua/programming/java/j4a_gui.html
Event Handling
2
¨ Java uses Event Driven Model for event handling.
¨ An event is generated when user interacts with GUI
¨ Some example events:
¤ Clicking a button
¤ Presing the ENTER key
¤ Typing a key
¤ Closing a frame
¤ Clicking a mouse button
Ozyegin University - CS 102 - Object Oriented Programming
Event Handling
3
¨ Once an event is generated, it is passed to other
objects which handle the event.
¨ This is also known as the Event Delegation Model.
Ozyegin University - CS 102 - Object Oriented Programming
Event Handling
4
¨ Three types of objects are involved
¤ Event Source
¤ Event Listener(s)
¤ Event Object
Ozyegin University - CS 102 - Object Oriented Programming
Event Handling
5
¨ Three types of objects are involved
¤ Event Source
n Usually components (like button or textfield) but they can be
other kind of objects (like windows) too.
n It registers to event listener(s)
n It generates event objects
¤ Event Listener(s)
¤ Event Object
Ozyegin University - CS 102 - Object Oriented Programming
Event Handling
6
¨ Three types of objects are involved
¤ Event Source
¤ Event Listener(s)
n Receives an event object from the event source and performs
the implemented approppriate action
¤ Event Object
Ozyegin University - CS 102 - Object Oriented Programming
Event Handling
7
¨ Three types of objects are involved
¤ Event Source
¤ Event Listener(s)
¤ Event Object
n Created by the event source and sent to the registered
event listener(s)
n It describes the event by encapsulating the necessary
information (like (x,y) coordinates in a mouse event)
Ozyegin University - CS 102 - Object Oriented Programming
Event Handling Process
8
Event Source Event Listener(s)
¨ An event source is registered with an event listener
Ozyegin University - CS 102 - Object Oriented Programming
Event Handling Process
9
Event Source Event Listener(s)
¨ User interacts with the event source. Triggers an
event.
Ozyegin University - CS 102 - Object Oriented Programming
Event Handling Process
10
Event Source Event Listener(s)
Event Object
¨ An event object is created.
Ozyegin University - CS 102 - Object Oriented Programming
Event Handling Process
11
Event Source Event Listener(s)
Event Object
¨ Appropriate listener is invoked
Ozyegin University - CS 102 - Object Oriented Programming
Event Handling
12
¨ Not procedural model.
¤ Code is not executed in a sequential manner.
¨ Sequential Model vs. Event Model
¤ Sequential model
¤ Event Model (Event loop)
¨ All event handling code executes in a single thread
¤ One event handler finishes before the next one can
start
Ozyegin University - CS 102 - Object Oriented Programming
Implementing Event Handlers
13
¨ There are three steps:
1. An event handler (event listener) class needs to be
declared
2. An instance of the event listener needs to be registered
to one or more components
3. The event handler class must implement the methods of
the interface
Ozyegin University - CS 102 - Object Oriented Programming
Listener Interfaces
14
An interface which
<<interface>>
every listener interface
EventListener
has to extend
<<interface>> <<interface>> <<interface>>
ActionListener MouseListener KeyListener
Ozyegin University - CS 102 - Object Oriented Programming
Listener Interfaces
15
<<interface>>
EventListener
<<interface>> <<interface>> <<interface>>
ActionListener MouseListener KeyListener
n Clicking a button fires an Action Event
n Clicking a mouse fires Mouse Event
n Typing a key fires Key Event.
Ozyegin University - CS 102 - Object Oriented Programming
Listener Interfaces
16
<<interface>>
EventListener
<<interface>> <<interface>> <<interface>>
ActionListener MouseListener KeyListener
These are the most commonly used ones.
There are also some others.
Ozyegin University - CS 102 - Object Oriented Programming
Listener Interfaces
17
<<interface>>
EventListener
<<interface>> <<interface>> <<interface>>
ActionListener MouseListener KeyListener
MyButtonListener MyKeyListener
Ozyegin University - CS 102 - Object Oriented Programming
Listener Interfaces
18
They contain the
<<interface>>
necessary code that
needs to be executed EventListener
when an event is
generated.
<<interface>> <<interface>> <<interface>>
ActionListener MouseListener KeyListener
MyButtonListener MyKeyListener
Ozyegin University - CS 102 - Object Oriented Programming
Listener Interfaces
19
<<interface>>
EventListener
Event Listener class needs to either
<<interface>> Øimplement
<<interface>> a listener interface
<<interface>>
ActionListener Øextend a class thatKeyListener
MouseListener implements a
listener interface
MyButtonListener MyKeyListener
Ozyegin University - CS 102 - Object Oriented Programming
ActionListener Interface
20
<<interface>>
ActionListener
+actionPerformed(event: ActionEvent)
¨ ActionListener interface contains actionPerformed
function.
¨ A listener implementing ActionListener needs to
implement this method.
¨ EventSource calls this method and sends the event as
a parameter.
Ozyegin University - CS 102 - Object Oriented Programming
21 Action Listener Examples
Ø EventExample1
Ø EventExample2
Ø EventExample3
Ø RadioButtonExample
Ø CheckBoxExample
Ø DynamicCheckBoxExample
Ozyegin University - CS 102 - Object Oriented Programming
EventExample1
22
¨ An example from last lecture.
¨ Lets extend it so that an event
occurs when we click the button
Ozyegin University - CS 102 - Object Oriented Programming
EventExample1
23
Ozyegin University - CS 102 - Object Oriented Programming
EventExample1
24
Implementing Event Handlers
25
¨ There are three steps:
1. An event handler (event listener) class needs to be
declared
2. An instance of the event listener needs to be registered
to one or more components
3. The event handler class must implement the methods of
the interface
Ozyegin University - CS 102 - Object Oriented Programming
EventExample1
26
EventExample1
27
EventExample1
28
addActionListener(ActionListener e)
functionality of JButton to register for
Action Listeners
EventExample1
29
Example (Before button click)
30
Example (After button click)
31
JOptionPane
32
¨ A simple dialog box for graphical input or output
¨ showMessageDialog displays a message similar to
System.out.println
¨ public static void showMessageDialog(
Component parent, Object message)
Ozyegin University - CS 102 - Object Oriented Programming
Example with JOptionPane
33
Example (After button click)
34
35 Action Listener Examples
Ø EventExample1
Ø EventExample2
Ø EventExample3
Ø RadioButtonExample
Ø CheckBoxExample
Ø DynamicCheckBoxExample
Ozyegin University - CS 102 - Object Oriented Programming
EventExample2
36
¨ When is clicked
¨ When is clicked
Ozyegin University - CS 102 - Object Oriented Programming
Multiple Listeners
37
¨ There are 2 buttons and 3 listeners.
¤ Button1 is registered to 2 listeners
n Listener1
n Listener2
¤ Button2 is registered to 2 listeners
n Listener2
n Listener3
¨ Remember an event source can register to multiple
event listeners.
Ozyegin University - CS 102 - Object Oriented Programming
Multiple Listener
38
¨ We have the same event listener class and three
listeners use this one.
¨ We need to use an id to diffirentiate them.
Ozyegin University - CS 102 - Object Oriented Programming
Multiple Listeners
39
Ozyegin University - CS 102 - Object Oriented Programming
Multiple Listeners
40
¨ actionPerformed function needs to be implemented
Ozyegin University - CS 102 - Object Oriented Programming
Multiple Listeners
41
¨ actionPerformed function needs to be implemented
Ø We need the listener id and the text inside the button.
Ø The listener id is a class instance
Ø The text of the button can be retrieved from event object
Ozyegin University - CS 102 - Object Oriented Programming
Event Objects
42
¨ Every event is a subclass of EventObject
¨ EventObject is an abstract class EventObject
¤ getSource() returns the event object #source: Object
¤ It returns the object reference, we +getSource(): Object
need to cast it before using it. +toString(): String
¨ Subclasses can add their own methods
Ozyegin University - CS 102 - Object Oriented Programming
Multiple Listeners
43
Ozyegin University - CS 102 - Object Oriented Programming
44 Action Listener Examples
Ø EventExample1
Ø EventExample2
Ø EventExample3
Ø RadioButtonExample
Ø CheckBoxExample
Ø DynamicCheckBoxExample
Ozyegin University - CS 102 - Object Oriented Programming
EventExample3
45
¨ Initial state
¨ When clicked to OK button or pressed ENTER in
textbox
Ozyegin University - CS 102 - Object Oriented Programming
EventExample3
46
¨ The event will copy the text inside textbox to the
label.
¨ Both the textField and label need to be accessed
from the event handler class.
Ozyegin University - CS 102 - Object Oriented Programming
EventExample3
47
Ozyegin University - CS 102 - Object Oriented Programming
EventExample3
48
¨ Why?
Ozyegin University - CS 102 - Object Oriented Programming
EventExample3
49
Ozyegin University - CS 102 - Object Oriented Programming
50 Action Listener Examples
Ø EventExample1
Ø EventExample2
Ø EventExample3
Ø RadioButtonExample
Ø CheckBoxExample
Ø DynamicCheckBoxExample
Ozyegin University - CS 102 - Object Oriented Programming
RadioButton Example
51
Ozyegin University - CS 102 - Object Oriented Programming
RadioButton Example
52
Ozyegin University - CS 102 - Object Oriented Programming
RadioButtons
53
¨ User can select all radiobuttons. How can restrict this?
Ozyegin University - CS 102 - Object Oriented Programming
ButtonGroup
54
¨ ButtonGroup component makes sure that only
button (radio button as well) can be selected at a
time.
Ozyegin University - CS 102 - Object Oriented Programming
RadioButton Example
55
Ozyegin University - CS 102 - Object Oriented Programming
RadioButton Example
56
¨ How many combinations of RadioButtons can a user
select?
Ozyegin University - CS 102 - Object Oriented Programming
RadioButton Example
57
¨ How many combinations of RadioButtons can a user
select?
Ozyegin University - CS 102 - Object Oriented Programming
RadioButton Example
58
¨ At this point report button does not do anything.
¨ Lets implement an event handler for the button.
Ozyegin University - CS 102 - Object Oriented Programming
RadioButton Example
59
¨ When the report button is
clicked display the
selected and unselected
radiobuttons.
Ozyegin University - CS 102 - Object Oriented Programming
RadioButton Example
60
¨ When the report button is
clicked display the
selected and unselected
radiobuttons.
¨ In our ActionListener class
we need to know which
radiobuttons are selected.
Ozyegin University - CS 102 - Object Oriented Programming
RadioButton Example
61
¨ When the report button is
clicked display the
selected and unselected
radiobuttons.
¨ In our ActionListener class
we need to know which
radiobuttons are selected.
¨ Therefore, we need to
send them to listener class.
Ozyegin University - CS 102 - Object Oriented Programming
RadioButton Example
62
Ozyegin University - CS 102 - Object Oriented Programming
RadioButton Example
63
Ozyegin University - CS 102 - Object Oriented Programming
RadioButton Example
64
¨ Do not forget to register the listener to the report
button.
Ozyegin University - CS 102 - Object Oriented Programming
65 Action Listener Examples
Ø EventExample1
Ø EventExample2
Ø EventExample3
Ø RadioButtonExample
Ø CheckBoxExample
Ø DynamicCheckBoxExample
Ozyegin University - CS 102 - Object Oriented Programming
CheckBox Example
66
¨ We can use CheckBox instead of RadioButton as
well.
Ozyegin University - CS 102 - Object Oriented Programming
CheckBox Example
67
Ozyegin University - CS 102 - Object Oriented Programming
CheckBox Example
68
Ozyegin University - CS 102 - Object Oriented Programming
CheckBox Example
69
Ozyegin University - CS 102 - Object Oriented Programming
CheckBox Example
70
Ozyegin University - CS 102 - Object Oriented Programming
CheckBox Example
71
Ozyegin University - CS 102 - Object Oriented Programming
CheckBox Example
72
¨ If we have not 5 but more CheckBoxes.
¨ Are we going to have more of the followings?
¨ Lets do the same thing dynamically...
Ozyegin University - CS 102 - Object Oriented Programming
73 Action Listener Examples
Ø EventExample1
Ø EventExample2
Ø EventExample3
Ø RadioButtonExample
Ø CheckBoxExample
Ø DynamicCheckBoxExample
Ozyegin University - CS 102 - Object Oriented Programming
DynamicCheckBox Example
74
Ozyegin University - CS 102 - Object Oriented Programming
DynamicCheckBox Example
75
DynamicCheckBox Example
76
Ozyegin University - CS 102 - Object Oriented Programming
DynamicCheckBox Example
77
Ozyegin University - CS 102 - Object Oriented Programming
MouseListener Interface
78
<<interface>>
MouseListener
+mousePressed(event: MouseEvent)
+mouseReleased(event: MouseEvent)
+mouseClicked(event: MouseEvent)
+mouseEntered(event: MouseEvent)
+mouseExited(event: MouseEvent)
Ozyegin University - CS 102 - Object Oriented Programming
<<interface>>
MouseListener Interface MouseListener
79
+mousePressed(event: MouseEvent)
+mouseReleased(event: MouseEvent)
+mouseClicked(event: MouseEvent)
+mouseEntered(event: MouseEvent)
+mouseExited(event: MouseEvent)
mousePressed Invoked when a mouse button has been
pressed on a component.
mouseReleased Invoked when a mouse button has been
released on a component.
mouseClicked Invoked when the mouse button has been
clicked (pressed and released) on a
component.
mouseEntered Invoked when the mouse enters a component.
mouseExited Invoked
Ozyegin University - CS 102 -when the mouse
Object Oriented exits a component.
Programming
MouseListener Interface
80
<<interface>>
MouseListener
+mousePressed(event: MouseEvent)
+mouseReleased(event: MouseEvent)
+mouseClicked(event: MouseEvent)
+mouseEntered(event: MouseEvent)
+mouseExited(event: MouseEvent)
¨ Do we need to implement them all?
Ozyegin University - CS 102 - Object Oriented Programming
Event Adapters
81
¨ Adapter classes provide empty implementations to
all the methods of interfaces.
¨ These abstract classes make interface usage easier
¨ Most event listener interfaces have their
corresponding adapter classes that have all of the
interface methods implemented.
Ozyegin University - CS 102 - Object Oriented Programming
Mouse Adapter
82
¨ This abstract class implements MouseListener
interface with empty bodies.
Ozyegin University - CS 102 - Object Oriented Programming
83 Mouse Listener Examples
GicikButton
MouseClickCoordinates
Ozyegin University - CS 102 - Object Oriented Programming
GicikButton
84
Ozyegin University - CS 102 - Object Oriented Programming
GicikButton
85
Ozyegin University - CS 102 - Object Oriented Programming
GicikButton
86
¨ setLocation function invokes setBounds function
but it uses the component’s current width and height.
Ozyegin University - CS 102 - Object Oriented Programming
87 Mouse Listener Examples
GicikButton
MouseClickCoordinates
Ozyegin University - CS 102 - Object Oriented Programming
MouseClickCoordinates
88
¨ A 400x400 frame
¨ Whereever a mouse
clicks on it, it prints out
the x,y coordinates of
the clicked location.
Ozyegin University - CS 102 - Object Oriented Programming
MouseClickCoordinates
89
Ozyegin University - CS 102 - Object Oriented Programming
MouseClickCoordinates
90
Ozyegin University - CS 102 - Object Oriented Programming
MouseClickCoordinates
91
Ozyegin University - CS 102 - Object Oriented Programming
MouseClickCoordinates
92
int getX()function returns the X position of the event
int getY()function returns the Y position of the event
int getButton()function returns the mouse button which has
changed the state
int getClickCount()function returns the number of mouse
clicks associated with
Ozyegin University the- event
- CS 102 withinProgramming
Object Oriented a certain time interval
94 Any Questions ?
Ozyegin University - CS 102 - Object Oriented Programming