0% found this document useful (0 votes)
11 views

Java Event Handling

Uploaded by

ushapetchi
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
11 views

Java Event Handling

Uploaded by

ushapetchi
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
You are on page 1/ 28

Java

Java Event
Event Handling
Handling
--

Mari Göransson - KaU - Datavete 1


nskap - DAVD11
Event Handling
• Happens every time a user
interacts with a user interface. For
example, when a user pushes a
button, or types a character.

Mari Göransson - KaU - Datavete 2


nskap - DAVD11
A Typical Situation:
Scrollbar
AWTEvent source interface
AdjustmentEvent Adjustable
<<use>> <<create>>

Component
EventListener ScrollBar
interface *
AdjustmentListener adjustmentListeners
+addAdjustmentListener:

InputScrollbarAdapter

+adjustmentValueChanged:
Mari Göransson - KaU - Datavete 3
nskap - DAVD11
Event Representation
• Each event is represented by an
object that gives information
about the event and identifies the
event source.
• In Java, every event is a subclass
of EventObject.

Mari Göransson - KaU - Datavete 4


nskap - DAVD11
Abstract class
EventObject
• getSource returns
the object on
which the event
occured.
• Each subclass
adds its own
specialized
methods.

Mari Göransson - KaU - Datavete 5


nskap - DAVD11
Event Sources
• Event sources are usually
components, but they can be
other kind of objects too.
• An event source can have multiple
event listeners registered on it.
• Examples of event sources are
e.g. buttons and windows.

Mari Göransson - KaU - Datavete 6


nskap - DAVD11
Event Listeners
• An event listener is an object that
wants to be notified when an
event has occured on a
component.
• The common interface that all
event listener interfaces must
extend is EventListener.

Mari Göransson - KaU - Datavete 7


nskap - DAVD11
Event Listeners
• For example, to listen for
ActionEvents the class must
implement the interface
ActionListener, that in turn is an
implementation of EventListener.
• An example is shown on the next
slide

Mari Göransson - KaU - Datavete 8


nskap - DAVD11
An ActionListener
Object Object
#source
EventObject

Component
AWTEvent

By the classes
TextComponent
implementing <<use>> ActionEvent <<create>>
the interface

EventListener
interface * TextField
ActionListener
+addActionListener:

ItemListener InputTextAdapter
AdjustmentListener
ConverterModel
+actionPerformed:

+itemStateChanged:
+actionPerformed:

Mari Göransson - KaU - Datavete 9


nskap - DAVD11
Event Adapters
• To highten the ease of use, most
eventlistener interfaces have
corresponding adapter classes
that have all of the interface
methods implemented.
• Must be subclassed.

Mari Göransson - KaU - Datavete 10


nskap - DAVD11
A Complex Listener
with Adapter
EventListener AWTEvent source interface
interface TreatmentEvent Treatable
TreatmentListener <<use>>
<<create>>
+treatment1Occured:
+treatment2Occured: * Manick
+treatmentnOccured: -listeners
-listeners:TreatmentListeners[*]

+addTreatmentListener:
ManickAdapter

+treatment1Occured: MySimpleListener
+treatment2Occured:
+treatmentnOccured:
+treatmentxOccured:

Mari Göransson - KaU - Datavete 11


nskap - DAVD11
Three Required Parts
• public class MyClass implements
ActionListener
• someComponent.addActionListene
r(aMyClass)
• public void
actionPerformed(ActionEvent e){}

Mari Göransson - KaU - Datavete 12


nskap - DAVD11
An Example

Mari Göransson - KaU - Datavete 13


nskap - DAVD11
Threads and Event
Handling
• All event handling code executes
in a single thread, the event-
dispatching thread.
• Ensures that one event handler
finishes before the next one can
start.

Mari Göransson - KaU - Datavete 14


nskap - DAVD11
Components and
Events
• Every component support
component, focus, key, mouse and
mouse-motion listeners.
• Fires only events for which
listeners has registered an interest
in.

Mari Göransson - KaU - Datavete 15


nskap - DAVD11
Java Event Handling
and the Observer
Pattern
• Java’s current event handling was
introduced in JDK 1.1.
• It is a specialized version of the
Observer pattern.

Mari Göransson - KaU - Datavete 16


nskap - DAVD11
Java Event Handling
and the Observer
Pattern
• The observer pattern is used when
an object wants to be informed of
a state change in another object.
• In java, the observer object listens
for events to happen.

Mari Göransson - KaU - Datavete 17


nskap - DAVD11
Java Event Handling
and the Observer
Pattern

Mari Göransson - KaU - Datavete 18


nskap - DAVD11
Java Event Handling
and the Observer
Pattern

Mari Göransson - KaU - Datavete 19


nskap - DAVD11
Java and the Observer
Pattern
• Java has included the Observer-
Observable pair of objects that
concurs to the Observer pattern, it
is therefore easy to implement the
pattern behaviour on your own.

Mari Göransson - KaU - Datavete 20


nskap - DAVD11
Events in the Converter
• ActionEvent
– Listener must implement the interface
ActionListener.
• ChangeEvent (Swing) or AdjustmentEvent
– Listener must implement the interface
ChangeListener or AdjustmentListener.
• ItemEvent
– Listener must implement the interface
ItemListener.

Mari Göransson - KaU - Datavete 21


nskap - DAVD11
ActionEvent
• Generated when the user presses
return in the textfield.
• EventSource calls the method
actionPerformed() in the listeners
and sends the event as a
parameter.

Mari Göransson - KaU - Datavete 22


nskap - DAVD11
ChangeEvent
• Generated when the user changes
the slider.
• Listeners must implement the
method stateChanged(), that is
invoked when the target of the
listener has changed its state.

Mari Göransson - KaU - Datavete 23


nskap - DAVD11
AdjustmentEvent
• Generated when the user changes
the value of the scrollbar (AWT).
• Has several specialized methods:
– getAdjustable()
– getAdjustmentType()
– getValue()

Mari Göransson - KaU - Datavete 24


nskap - DAVD11
AdjustmentEvent
• Listeners must implement the
method
adjustmentValueChanged() that is
invoked when the srollbar changes
its value.

Mari Göransson - KaU - Datavete 25


nskap - DAVD11
ItemEvent
• Generated when the user selects
another unit in the choicebox.
• Has some specialized methods:
– getItem()
– getItemSelectable()
– getStateChanged()

Mari Göransson - KaU - Datavete 26


nskap - DAVD11
The ItemEvent
Structure source interface
AWTEvent ItemSelectable
ItemEvent

<<use>> <<create>>

EventListener
interface * Component
ItemListener Choice

+addItermListener:

ActionListener
AdjustmentListener
ConverterModel

+itemStateChanged:
+actionPerformed:
Mari Göransson - KaU - Datavete 27
nskap - DAVD11
ItemEvent
• The event is passed to every
ItemListener object which registered to
receive such events using the
component's addItemListener()
method.
• When an item-selection event occurs,
the listener object's
itemStateChanged() method is invoked.

Mari Göransson - KaU - Datavete 28


nskap - DAVD11

You might also like