Java Event Handling - PowerPoint PPT Presentation

About This Presentation
Title:

Java Event Handling

Description:

Mari G ransson - KaU - Datavetenskap - DAVD11. 1. Java Event Handling ... Mari G ransson - KaU - Datavetenskap - DAVD11. 11. A Complex Listener with Adapter ... – PowerPoint PPT presentation

Number of Views:88
Avg rating:3.0/5.0
Slides: 29
Provided by: marigr
Category:
Tags: event | handling | java | mari

less

Transcript and Presenter's Notes

Title: Java Event Handling


1
Java Event Handling
  • --

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

3
A Typical Situation Scrollbar
4
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.

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

6
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.

7
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.

8
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

9
An ActionListener
10
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.

11
A Complex Listener with Adapter
12
Three Required Parts
  • public class MyClass implements ActionListener
  • someComponent.addActionListener(aMyClass)
  • public void actionPerformed(ActionEvent e)

13
An Example
14
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.

15
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.

16
Java Event Handling and the Observer Pattern
  • Javas current event handling was introduced in
    JDK 1.1.
  • It is a specialized version of the Observer
    pattern.

17
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.

18
Java Event Handling and the Observer Pattern
19
Java Event Handling and the Observer Pattern
20
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.

21
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.

22
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.

23
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.

24
AdjustmentEvent
  • Generated when the user changes the value of the
    scrollbar (AWT).
  • Has several specialized methods
  • getAdjustable()
  • getAdjustmentType()
  • getValue()

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

26
ItemEvent
  • Generated when the user selects another unit in
    the choicebox.
  • Has some specialized methods
  • getItem()
  • getItemSelectable()
  • getStateChanged()

27
The ItemEvent Structure
28
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.
Write a Comment
User Comments (0)
About PowerShow.com