Download presentation
Presentation is loading. Please wait.
1
Object-Oriented Programming with Java The Java Event Model Lecture 5
2
Callbacks and Events Contents Callbacks Events Events in Java
3
Pauls Pictures Pauls Documents Pauls Sums Pauls Homework Problems Pauls Pictures Pauls Documents Pauls Sums Pauls Homework Pauls ToDo Lists
4
My Documents Reports Papers Presentations Client Window Library File System Calls But I need to tell you something! Slide Shows I’m in charge here, guys!
5
Callbacks Callbacks are used in procedural libraries when they need to handle asynchronous events The alternative is for the client to continuously poll the library for events This is inefficient, especially if a large number of library components are registered with a client This is inefficient, especially if a large number of library components are registered with a client But the use of callback means a client may observe “intermediate” states of a library “Classically” a library’s operations would run to completion before returning control “Classically” a library’s operations would run to completion before returning control
6
Call Sequence ClientaUserLibrary Client installs callback Third party calls library Library invokes callback “I need to tell you something!” “What’s happened?” “He’s changed a name!” Callback queries library “That’s cool” Callback returns Library returns
7
Directory Service public class Directory { public void addEntry(String name, File file) { // pre name != “” and file != null // post File file = map.get(name) } public void removeEntry(String name) { // pre name != “” // post map.get(name) = nil } public void registerNotifier(Notifier n) { // pre n != nil // post n registered, will be called on addEntry and removeEntry }
8
Callbacks and Events Contents Callbacks Events Events in Java
9
Events An abstraction of Callback that is applicable to “federations” of interacting components The firing of an event is a way of one object telling one or more other recipients that something interesting has happened The sender fires an event The sender fires an event A recipient is called a listener and handles the event A recipient is called a listener and handles the event
10
Java Event Model Event Source Event Listener Register Event Listener Fire Event Event Object Event Object
11
Event Objects Encapsulates information specific to an instance of an event E.g. a “mouse click” event may contain: The position of the mouse pointer The position of the mouse pointer Which mouse button was clicked (and how many times) Which mouse button was clicked (and how many times) The event object is passed as a parameter to the event notification method
12
Event Listeners These are objects that need to be notified when a certain event occurs Event notifications are made through method invocations in the listening object The event object is passed as a parameter The event object is passed as a parameter The event source must know which listener object(s) to call The event source must know which listener object(s) to call This information is contained in an event- listener interface
13
Event Sources Objects that fire events Implement methods that allow listeners to: Register their interest in the events they generate; Register their interest in the events they generate; Unregister their interest in the events they generate. Unregister their interest in the events they generate. Multicast event delivery enables an event to be fired to a number of event-listeners
14
Summary EventObject source getSource() toString() EventListener notification(evt) EventSource addListener() removeListener() fires passed to registers 0..* invokes notifications in0..*
15
Callbacks and Events Contents Callbacks Events Events in Java
16
Java Events An “Event” encapsulates information about some state change in its source Pressing a button Pressing a button Entering text in a text field Entering text in a text field Moving a slider on a scroll bar, … Moving a slider on a scroll bar, … Events need not be generated by users: Expiration of a timer Expiration of a timer Arrival of incoming data, … Arrival of incoming data, …
17
Event Sources An event source must: Provide methods to enable “listeners” to register and unregister interest in its events Provide methods to enable “listeners” to register and unregister interest in its events Fire the events, of course! Fire the events, of course! “Send” each event to all listeners who are interested in that type of event “Send” each event to all listeners who are interested in that type of event For an event of Type, and listener e1: public void public void addTypeListener(TypeListener e1); public void public void removeTypeListener(TypeListener e1);
18
Listeners Have three responsibilities: Register for events of a certain Type Register for events of a certain Type Implement an interface to “receive” events of a certain Type Implement an interface to “receive” events of a certain Type Unregister if they no longer wish to receive events of Type Unregister if they no longer wish to receive events of Type
19
Events EventObject: EventObject(Object source) Object getSource( ) String toString( ) AWTEvent: AWTEvent(Object source, int id) int getID( ) String toString( ) Object EventObject AWTEvent
20
Example: Temperature Converter // Event Source convertTemp = new JButton("Convert..."); … // Listen to events from Convert button. convertTemp.addActionListener(this);… // Implementation of ActionListener interface. // Implementation of ActionListener interface. public void actionPerformed(ActionEvent event) { public void actionPerformed(ActionEvent event) { // Parse degrees Celsius as a double and convert to Fahrenheit. // Parse degrees Celsius as a double and convert to Fahrenheit. int tempFahr = (int)((Double.parseDouble(tempCelsius.getText())) int tempFahr = (int)((Double.parseDouble(tempCelsius.getText())) * 1.8 + 32); * 1.8 + 32); fahrenheitLabel.setText(tempFahr + " Fahrenheit"); }
21
Summary The components of a software product must interact to achieve a goal In general, it is hard to identify a single component that has overall control So a general model is for components to interact by triggering “Events” that other components register an interest in
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.