Listener Interfaces  A mechanism for registering handlers that respond to external events  Motivation: Develop a Canvas class that:  supports drawing.

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 Graphical User Interface (GUI) Applications Abstract Windowing Toolkit (AWT) Events Handling Applets.
Introduction to Java 2 Programming
Event Handling.
Event Handling Events and Listeners Timers and Animation.
CS3157 Java UI Recitation. Material Covered: Overview of AWT components, Event Handling, creating applets, and sample UI. Not covered in recitation: Drawing,
Event Handling. In this class we will cover: Keyboard Events Mouse Events Focus Events Action Interface Multicasting.
EVENTS CSC 171 FALL 2004 LECTURE 16. “Traditional” Input In console applications, user input is under control of the program The program asks the user.
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.
Event Handling in Java CSE Software Engineering Spring 2000 By Prasad.
Events in Java Swing Chris North cs3724: HCI. Typical command line program Non-interactive Linear execution program: main() { code; }
Mouse Events. Handling Mouse Events Java provides two listener interfaces to handle mouse events: MouseListener;  MouseListener;  MouseMotionListener.
David Streader Computer Science Victoria University of Wellington Copyright: David Streader, Victoria University of Wellington GUI and the UI API COMP.
Java Programming, 3e Concepts and Techniques Chapter 3 Section 65 – Manipulating Data Using Methods – Java Applet.
Welcome to CIS 083 ! Events CIS 068.
Lecture 5: Interaction 1  Principles of Interactive Graphics  CMSCD2012  Dr David England, Room 711,  ex 2271 
More Event Handling Adapters Anonymous Listeners Pop menus Validating User Input.
Java GUI’s are event driven, meaning they generate events when the user interacts with the program. Typical events are moving the mouse, clicking a mouse.
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.
GUIs in Java Swing, Events CS2110, SW Development Methods Readings: MSD, Chapter 12 Lab Exercise.
 2002 Prentice Hall, Inc. All rights reserved Introduction Graphical User Interface (GUI) –Gives program distinctive “look” and “feel” –Provides.
– Advanced Programming P ROGRAMMING IN Lecture 21 Introduction to Swing.
1 Outline 1 Introduction 2 Overview of Swing Components 3 JLabel 4 Event Handling 5 TextFields 6 How Event Handling Works 7 JButton 8 JCheckBox and JRadioButton.
Graphic User Interface. Graphic User Interface (GUI) Most of us interact with computers using GUIs. GUIs are visual representations of the actions you.
Graphics and Event-Driven Programming in Java John C. Ramirez Department of Computer Science University of Pittsburgh.
MSc Workshop - © S. Kamin, U.Reddy Lect 4 - Events - 1 Lecture 4 – Event Handling r Painting r Event types r Catching different event types.
COMP 321 Week 2. Outline Event-Driven Programming Events, Event Sources, Event Listeners Button and Timer Events Mouse Events, Adapters.
Xiaoying Gao Computer Science Victoria University of Wellington Copyright: Xiaoying Gao, Peter Andreae, Victoria University of Wellington More Interfaces.
Introduction to Java Beans CIS 421 Web-based Java Programming.
Liang, Introduction to Java Programming, Fifth Edition, (c) 2005 Pearson Education, Inc. All rights reserved Chapter 12 Event-Driven Programming.
For (int i = 1; i
© Copyright by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Outline 21.1 Test-Driving the Painter Application.
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
Event Handling. 2 GUIs are event driven –Generate events when user interacts with GUI e.g., moving mouse, pressing button, typing in text field, etc.
CS1054: Lecture 21 - Graphical User Interface. Graphical User Interfaces vs. Text User Interface.
Introduction to Java Programming
Object Oriented Programming.  Interface  Event Handling.
Agenda Introduction. Event Model. Creating GUI Application. Event Examples.
1 CSE 331 Model/View Separation and Observer Pattern slides created by Marty Stepp based on materials by M. Ernst, S. Reges, D. Notkin, R. Mercer, Wikipedia.
Creating User Interfaces Event-Driven Programming.
1 Event Handling – Lecture 4 Prepared by: Ahmad Ramin Rahimee Assistant Professor ICTI.
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.
1 Chapter 3 Event-Driven Programming. 2 Objectives F To explain the concept of event-driven programming (§12.2). F To understand event, event source,
CSI 3125, Preliminaries, page 1 Event Handling. CSI 3125, Preliminaries, page 2 Event Handling An Event Change in the state of an object is known as 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.
1 Introduction to J2ME Outline MIDP Building J2ME Apps- Tool J2ME Wireless Toolkit Demo MIDlet Programming -- MIDlet Transition States -- Midlet Skeleton.
Swing GUI Components So far, we have written GUI applications which can ‘ draw ’. These applications are simple, yet typical of all Java GUI applications.
1 Lecture 8: User Interface Components with Swing.
12-Jun-16 Event loops. 2 Programming in prehistoric times Earliest programs were all “batch” processing There was no interaction with the user Input Output.
Prepared by: Dr. Abdallah Mohamed, AOU-KW Unit7: Event-driven programming 1.
GUI Programming using Java - Event Handling
Events and Event Handling
CompSci 230 S Programming Techniques
Chapter 12 Event-Driven Programming
Introduction to Event-Driven Programming
Event loops 16-Jun-18.
Programming in Java Event Handling
GUI Event Handling Nithya Raman.
GUI Programming III: Events
Event-driven programming for GUI
Introduction to Computing Using Java
Event loops.
Event loops 17-Jan-19.
Event loops 17-Jan-19.
Event loops 8-Apr-19.
Tonga Institute of Higher Education
Event loops.
Event loops 19-Aug-19.
Presentation transcript:

Listener Interfaces  A mechanism for registering handlers that respond to external events  Motivation: Develop a Canvas class that:  supports drawing of 2D primitives (lines, circles, rectangles)  detects input events (i.e. events triggered by the mouse, keyboard, etc.)  allows users to handle these events based on their application

Listener Interfaces  A mechanism for registering handlers that respond to external events  Motivation: Develop a Canvas class that:  supports drawing of 2D primitives (lines, circles, rectangles)  detects input events (i.e. events triggered by the mouse, keyboard, etc.)  allows users to handle these events based on their application

Listener Interfaces  A mechanism for registering handlers that respond to external events  Motivation: Develop a Canvas class that:  supports drawing of 2D primitives (lines, circles, rectangles)  detects input events (i.e. events triggered by the mouse, keyboard, etc.)  allows users to handle these events based on their application  How can we separate the implementation of the Canvas from the implementation of the user code for handling the events

Listener Interfaces  How can we separate the implementation of the Canvas from the implementation of the user code fro handling events  Use a “registration” mechanism of event handlers:  user provides an object/handler to be notified when an event occurs  the Canvas is only responsible for notifying the handler of the event  the actual code of processing the event is in the user-supplied handler

Listener Interfaces  How can we separate the implementation of the Canvas from the implementation of the user code fro handling events  Use a “registration” mechanism of event handlers:  user provides an object/handler to be notified when an event occurs  the Canvas is only responsible for notifying the handler of the event  the actual code of processing the event is in the user-supplied handler  Need a set of agreed upon methods that handler must implement  Implication: The Canvas class only knows how to communicate with handler, not what handler does

Java Swing Example  MouseListener: set of agreed upon methods for mouse input handling: interface MouseListener { void mouseClicked(MouseEvent e); void mouseEntered(MouseEvent e); }  MouseEvent: “data structure” for describing the event  JPanel: representation of Canvas from Java Swing package  registration: void addMouseListener(MouseListener l)  notification: void processMouseEvent(MouseEvent e)

Java Swing Example  User creates a class that implements MouseListener interface class MyListener implements MouseListener { void mouseClicked(MouseEvent e) {... User-specific code for event handling... } void mouseEntered(MouseEvent e) {... User-specific code for event handling... } }

Java Swing Example panel listeners handler JPanel panel = new JPanel(“Demo”); MyListener handler = new MyListener(); panel.addMouseListener(handler);

J2ME Form Example  CommandListener: set of agreed upon methods for command handling interface CommandListener { void commandAction(Command c, Displayable d); }  Command: “data structure” for describing the event  Form: the application view  registration: void setCommandListener (CommandListener l)  notification: some method in Form class

J2ME Form Example  User creates a class that implements CommandListener interface class MyListener implements CommandListener { void commandAction(Command c, Displayable d) {... User-specific code for event handling... }

J2ME form Example panel listeners handler Form panel = new Form(“Demo”); MyListener handler = new MyListener(); panel.setCommandListener(handler);

J2ME User Interface Library Displayable ScreenCanvas FormAlertListTextbox Item DateFieldImageItemTextFieldStringItemGaugeChoiceGroup

Lab Exercise  Create a simple Form with a TextField and three Commands:  Exit – quits the application  Add Item – shows the text in the TextField on the Form  Delete Item – removes the last text item from the Form