Object-Oriented Programming with Java The Java Event Model Lecture 5.

Slides:



Advertisements
Similar presentations
Event handling and listeners What is an event? user actions and context event sources and listeners Why should my programs be event- driven? User interaction.
Advertisements

1 Event Listeners Some Events and Their Associated Event Listeners Act that Results in the EventListener Type User clicks a button, presses Enter while.
Mari Göransson - KaU - Datavetenskap - DAVD11 1 Java Event Handling --
Event Handling.
CS 178: Programming with Multimedia Objects Aditya P. Mathur Professor of Computer Sciences Purdue University, West Lafayette Sept 9, 2004 Last update:
Component-Based Software Engineering Components and Interfaces Paul Krause.
Event Handling Events and Listeners Timers and Animation.
Unit 12 Object-oriented programming: Event-driven programming for GUI Jin Sa.
1 Event Driven Programming with Graphical User Interfaces (GUIs) A Crash Course © Rick Mercer.
Writing GUI Applications An introduction with Java.
Object-Oriented Programming with Java Lecture 2 The Java Event Model.
Components, interfaces, and re-entrance Taciana Amorim Vanderlei
Lecture 18 Review the difference between abstract classes and interfaces The Cloneable interface Shallow and deep copies The ActionListener interface,
Object-Oriented Analysis and Design
GUI Event Handling Nithya Raman. What is an Event? GUI components communicate with the rest of the applications through events. The source of an event.
Internet Software Development The Java Event Model Lecture 5.
Io package as Java’s basic I/O system continue’d.
A.k.a. GUI’s.  If you want to discuss your Lab 2 grade come see me this week. ◦ Office: 436 ERB. One hour prior to class ◦ Open to Appointments MWF 
ACM/JETT Workshop - August 4-5, ExceptionHandling and User Interfaces (Event Delegation, Inner classes) using Swing.
Welcome to CIS 083 ! Events CIS 068.
Component-Based Software Engineering Using Interfaces Paul Krause.
Java Programming: From Problem Analysis to Program Design, Second Edition1  Learn about basic GUI components.  Explore how the GUI components JFrame,
MSc/PgDip in ITIntroductory Programming: Week 4 Lecture 21 INTRODUCTORY PROGRAMMING Week 4 Lecture 2 Fonts –Horstmann 4.6 ActionEvents –different events.
MIT AITI 2003 Lecture 17. Swing - Part II. The Java Event Model Up until now, we have focused on GUI's to present information (with one exception) Up.
1 Event Driven Programs Rick Mercer. 2 So what happens next?  You can layout a real pretty GUI  You can click on buttons, enter text into a text field,
FLTK Help Session By Richard Yu Gu CS 638 -Graphics Fall, 1999.
C H A P T E R T E N Event-Driven Programming Programming Languages – Principles and Paradigms by Allen Tucker, Robert Noonan.
Carnegie Mellon University MSCF1 C#/.NET Basics 2 Some code is from “C# in a Nutshell”
Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rights reserved Chapter 14 Event-Driven Programming.
Dale Roberts GUI Programming using Java - Event Handling Dale Roberts, Lecturer Computer Science, IUPUI Department of Computer.
Event Handling Mohanraj S AP / IT Angel College of Engg & Tech.,
GUIs in Java Swing, Events CS2110, SW Development Methods Readings: MSD, Chapter 12 Lab Exercise.
Copyright © 2002, Systems and Computer Engineering, Carleton University EventModel.ppt * Object-Oriented Software Development Part 17.
Introduction to Java Beans CIS 421 Web-based Java Programming.
Object-Oriented Program Development Using Java: A Class-Centered Approach, Enhanced Edition.
Creating a GUI with JFC/Swing. What are the JFC and Swing? JFC –Java Foundation Classes –a group of features to help people build graphical user interfaces.
UID – Event Handling and Listeners Boriana Koleva
CS-1020 Dr. Mark L. Hornick 1 Event-Driven Programming.
Object Oriented Programming.  Interface  Event Handling.
CSE 331 Software Design & Implementation Hal Perkins Autumn 2012 Event-Driven Programming 1.
Agenda Introduction. Event Model. Creating GUI Application. Event Examples.
Liang, Introduction to Java Programming, Sixth Edition, (c) 2005 Pearson Education, Inc. All rights reserved Chapter 27 JavaBeans and.
Index Event Handling Events Event Source Event Listener Event Classes Action Event Class Adjustment event Class Event Source Event Listener Interface Using.
Introduction to visual programming C#. Learning Outcomes In this chapter, you will learn about :  Event-Based Programming  The Event Based Model  Application.
Java Programming: From Problem Analysis to Program Design, 3e Chapter 6 Graphical User Interface (GUI) and Object-Oriented Design (OOD)
Java Programming: From Problem Analysis to Program Design, Second Edition1 Lecture 5 Objectives  Learn about basic GUI components.  Explore how the GUI.
(1) Introduction to Java GUIs Philip Johnson Collaborative Software Development Laboratory Information and Computer Sciences University of Hawaii Honolulu.
Lecture 18: Events; Cool Applets Yoni Fridman 7/30/01 7/30/01.
1 Event Driven Programs Rick Mercer. 2 So what happens next?  You can layout a real pretty GUI  You can click on buttons, enter text into a text field,
What Is an Event? Events – Objects that describe what happened Event sources – The generator of an event Event handlers – A method that receives an event.
Mouse Events GUI. Types of Events  Below, are some of the many kinds of events, swing components generate. Act causing EventListener Type User clicks.
Java - hello world example public class HelloWorld { public static void main (String args[]) { System.out.println("Hello World"); }
Lesson 28: More on the GUI button, frame and actions.
Events. Slide 2©SoftMoore Consulting Events Events are generated when a user interacts with the view objects of an application. Examples –button clicked–
MIT AITI 2004 Swing Event Model Lecture 17. The Java Event Model In the last lecture, we learned how to construct a GUI to present information to the.
5-1 GUIs and Events Rick Mercer. 5-2 Event-Driven Programming with Graphical user Interfaces  Most applications have graphical user interfaces to respond.
1 Event Driven Programming with Graphical User Interfaces (GUIs) A Crash Course © Rick Mercer.
Component-Based Software Engineering Components and Interfaces Paul Krause and Sotiris Moschoyiannis.
GUI Programming using Java - Event Handling
CSC 205 Programming II Lecture 5 AWT - I.
Events and Event Handling
CompSci 230 S Programming Techniques
A First Look at GUI Applications
Observer Design Pattern
Programming in Java Event Handling
Ellen Walker Hiram College
Event-driven programming for GUI
Events, Event Handlers, and Threads
Constructors, GUI’s(Using Swing) and ActionListner
Component-Based Software Engineering
Presentation transcript:

Object-Oriented Programming with Java The Java Event Model Lecture 5

Callbacks and Events Contents  Callbacks  Events  Events in Java

 Pauls Pictures  Pauls Documents  Pauls Sums  Pauls Homework Problems  Pauls Pictures  Pauls Documents  Pauls Sums  Pauls Homework  Pauls ToDo Lists

 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!

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

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

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 }

Callbacks and Events Contents  Callbacks  Events  Events in Java

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

Java Event Model Event Source Event Listener Register Event Listener Fire Event Event Object Event Object

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

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

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

Summary EventObject source getSource() toString() EventListener notification(evt) EventSource addListener() removeListener() fires passed to registers 0..* invokes notifications in0..*

Callbacks and Events Contents  Callbacks  Events  Events in Java

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, …

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);

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

Events  EventObject: EventObject(Object source) Object getSource( ) String toString( )  AWTEvent: AWTEvent(Object source, int id) int getID( ) String toString( ) Object EventObject AWTEvent

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())) * ); * ); fahrenheitLabel.setText(tempFahr + " Fahrenheit"); }

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