1CS480: Graphical User Interfaces. Dario Salvucci, Drexel University. Lecture 6: Event-Driven Programming.

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.
Liang,Introduction to Java Programming,revised by Dai-kaiyu 1 Chapter 12 Event-Driven Programming 找出画中真谛 — 保罗. 塞尚.
Jan Event Handling -1.1 Yangjun Chen Dept. Business Computing University of Winnipeg.
Event Handling. In this class we will cover: Basics of event handling The AWT event hierarchy Semantic and low-level events in the AWT.
Liang, Introduction to Java Programming, Seventh Edition, (c) 2009 Pearson Education, Inc. All rights reserved Chapter 15 Event-Driven Programming.
Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rights reserved Chapter 14 Event-Driven Programming.
Events ● Anything that happens in a GUI is an event. For example: – User clicks a button, presses return when typing text, or chooses a menu item ( ActionEvent.
For IST410 Students only Events-1 Event Handling.
Unit 111 Event-Driven Programming Listener or Event handler Example: Handling Button Events Example: Handling Mouse Events Example: Handling Keyboard Events.
Event-Driven Programming
Unit 12 Object-oriented programming: Event-driven programming for GUI Jin Sa.
Event Handling n Events: an event is an object that describes a state change in a source. n Event Sources: A source is an object that generates an event.
Event-Driven Programming
1 Gui Programming (Part I) Graphical User Interfaces (Part I) l Introduction to events. l A Brief history. l Event sources and listeners. l The delegation.
Graphical User Interface (GUI) Nelson Padua-Perez Chau-Wen Tseng Department of Computer Science University of Maryland, College Park.
Intermediate Java1 An example that uses inner classes Week Four Continued.
1 lecture 12Lecture 13 Event Handling (cont.) Overview  Handling Window Events.  Event Adapters Revisited.  Introduction to Components and Containers.
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.
3461 Sequential vs. Event-driven Programming Reacting to the user.
28-Aug-15 Air Force Institute of Technology Electrical and Computer Engineering Object-Oriented Programming Design Topic : Event Handling – GUI Part II.
ACM/JETT Workshop - August 4-5, ExceptionHandling and User Interfaces (Event Delegation, Inner classes) using Swing.
1 CSC111H Graphical User Interfaces (GUIs) Introduction GUIs in Java Understanding Events A Simple Application The Containment Hierarchy Layout Managers.
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.
Java GUI CSCE 190 – Java Instructor: Joel Gompert Mon, July 26, 2004.
More Event Handling Adapters Anonymous Listeners Pop menus Validating User Input.
Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rights reserved Chapter 14 Event-Driven Programming.
CSE 501N Fall ‘09 20: Event Handling and Inner Classes 17 November 2009 Nick Leidenfrost.
Ch 3-4: GUI Basics Java Software Solutions Foundations of Program Design Sixth Edition by Lewis & Loftus Coming up: GUI Components.
COMP 321 Week 2. Outline Event-Driven Programming Events, Event Sources, Event Listeners Button and Timer Events Mouse Events, Adapters.
Liang, Introduction to Java Programming, Fifth Edition, (c) 2005 Pearson Education, Inc. All rights reserved Chapter 12 Event-Driven Programming.
Pravin Yannawar, DOCS, NMU Jalgaon. Basic Java : Event handling in AWT and Swing 2 Objectives of This Session Explain the Event handling mechanism & demonstrate.
Object-Oriented Program Development Using Java: A Class-Centered Approach, Enhanced Edition.
Event Handling. Event Driven Programming Flow of programs is determined by events. The Operating system recognizes events and passes them to the particular.
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.
CS-1020 Dr. Mark L. Hornick 1 Event-Driven Programming.
Anonymous Classes An anonymous class is a local class that does not have a name. An anonymous class allows an object to be created using an expression.
Lesson 6 Programming Techniques Event Handling /EvH/ AUBG ICoSCIS Team Assoc. Prof. Stoyan Bonev March, , 2013 SWU, Blagoevgrad.
Object Oriented Programming.  Interface  Event Handling.
Event Handling. The signals that a program receives from the operating system as a result of the actions are called events. A window based program is.
Agenda Introduction. Event Model. Creating GUI Application. Event Examples.
Ch13 Creating windows and applets. Short overview AWT (Abstract Windowing Toolkit) Early Java development used graphic classesEarly Java development used.
Index Event Handling Events Event Source Event Listener Event Classes Action Event Class Adjustment event Class Event Source Event Listener Interface Using.
CIS Intro to JAVA Lecture Notes Set 8 9-June-05.
Event-Driven Programming CSCI 201L Jeffrey Miller, Ph.D. HTTP :// WWW - SCF. USC. EDU /~ CSCI 201 USC CSCI 201L.
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.
1CS480: Graphical User Interfaces. Dario Salvucci, Drexel University. Lecture 3: Layout Basics.
Event Handling and Listeners in SWING The practice of event handling.
Swing GUI Components So far, we have written GUI applications which can ‘ draw ’. These applications are simple, yet typical of all Java GUI applications.
Event Handling CS 21a: Introduction to Computing I First Semester,
UQC117S2 Graphics Programming Lecture 2 Event Handling Program as a sequence of instructions Event -driven program Need to detect the event and carry out.
Sep 181 Example Program DemoTranslateEnglishGUI.java.
1 DemoBasic_v3, DemoBasic_v4 JButton JLabel. 2 Registering an ActionListener Register by invoking the following from within constructor DemoBasicFrame.
Chapter 14 Event-Driven Programming
CHAPTER Reacting to the user.
Chapter 12 Event-Driven Programming
Event Handling Chapter 2 Objectives
Programming in Java Event Handling
Ellen Walker Hiram College
GUI Event Handling Nithya Raman.
Event-driven programming for GUI
Introduction to Computing Using Java
Chapter 16 Event-Driven Programming
Constructors, GUI’s(Using Swing) and ActionListner
Presentation transcript:

1CS480: Graphical User Interfaces. Dario Salvucci, Drexel University. Lecture 6: Event-Driven Programming

CS338: Graphical User Interfaces. Michael Czajkowski, Drexel University.1 Handling GUI events Ok, you’ve made a basic GUI. Now the user does something — an event occurs. How do you handle the event? Wait, what events are we talking about? User clicks a button, presses Return while typing in a text field, or chooses a menu item User closes a frame (main window) User presses a mouse button while the cursor is over a component User moves the mouse over a component Component becomes visible Component gets the keyboard focus Table or list selection changes

CS338: Graphical User Interfaces. Michael Czajkowski, Drexel University.1 Programming the GUI Sequential/procedural programming – your program is (almost) always in control – for user input, the program dictates when/how, and the user responds yourFoo() yourSubFoo() systemFoo() yourSubFoo() yourFoo()

CS338: Graphical User Interfaces. Michael Czajkowski, Drexel University.1 Programming the GUI Sequential/procedural programming (cont.) – the good points easy to think about: one event, then the next, … easy to design and represent with well-known models easy to program with today’s programming languages – the big bad point program dictates when/how user must respond Great for the programmer… What about the user?!?!?

CS338: Graphical User Interfaces. Michael Czajkowski, Drexel University.1 Programming the GUI Event-driven programming – system / toolkit handles much of processing – events on a queue, handled in turn – advantages flexible interaction — user decides when/how easier coding — you code only the “important” stuff better for the programmer and the user! MAIN LOOP yourClickHandler() yourScrollHandler() yourKeyHandler()...

CS338: Graphical User Interfaces. Michael Czajkowski, Drexel University.1 Ways to handle events “Macintosh-style” event handling – decide what the event is – figure out what window it goes to – handle the event void handleEvent (event) { switch (event->type) { case MOUSE_CLICK: window = event->window; > case …: } Note: Pseudocode!

CS338: Graphical User Interfaces. Michael Czajkowski, Drexel University.1 Ways to handle events Listener model – create widget, register listener function – listener function called for each event void myButtonClickCallback (window, other_data) { … } void main () { button = new Button (label, …); registerCallback (button, CLICK_CALLBACK, &(myButtonClickCallback)); } NB: Pseudocode!

CS338: Graphical User Interfaces. Michael Czajkowski, Drexel University.1 Doing the Math, reveals... Object-oriented event handling – the way Java does it! – based on the OOP component hierarchy define event-handling methods for any object, just not the components! components can (of course) inherit these methods from parent components, same for all objects. – Based on the Model-View-Control Architecture. Listeners are the Control. – now let’s look at this in detail…

CS338: Graphical User Interfaces. Michael Czajkowski, Drexel University.1 Model-View-Controller Architecture What defines a component? In Swing (and similar frameworks), a component has three crucial elements: – model: what data is associated with component – view: how the component is displayed on-screen – controller: how the component responds to user interaction / events For example, a JTextArea component – Model: The text, stored in a String object. – View: A white window with text cursor. – Controller: A listener that updates the String

CS338: Graphical User Interfaces. Michael Czajkowski, Drexel University.1 Model-View-Controller Architecture Another Example: the scrollbar How about a menu? a toolbar? Model min = 0 max = 255 value = 87 ViewController mouse click on end mouse click on bar mouse drag on scroller

CS338: Graphical User Interfaces. Michael Czajkowski, Drexel University.1 Model-View-Controller Architecture All three elements are interdependent! Why is this breakdown useful? – multiple components can tied to same model – models can have different “look & feel”s Model View Controller Component User Conduit

CS338: Graphical User Interfaces. Michael Czajkowski, Drexel University.1 The Model Is The Program Models are your internal representation of the state of the program. Use any data structure you want, its up to the Component to visualize it. You program the Component to display it the way you want! You program the Model to store the data (hopefully efficiently) the way you want!

CS338: Graphical User Interfaces. Michael Czajkowski, Drexel University.1 Event Listeners, the definitions. Event sources generate events Event listeners respond to them User clicks a button, presses Return whileActionListener typing in a text field, or chooses a menu item User closes a frame (main window)WindowListener User presses a mouse button while the cursorMouseListener is over a component User moves the mouse over a componentMouseMotionListener All of these are in the JavaDocs!

CS338: Graphical User Interfaces. Michael Czajkowski, Drexel University.1 ActionEvent A class with three methods: We won’t use these methods at the moment, but the keep the class in mind! String getActionCommand (); int getModifiers (); String paramString ();

CS338: Graphical User Interfaces. Michael Czajkowski, Drexel University.1 ActionListener An interface with a single method: We implement the interface as follows: public interface ActionListener { void actionPerformed (ActionEvent e); } public class MyClassThatListens … implements ActionListener { … public void actionPerformed (ActionEvent e) { … } … }

CS338: Graphical User Interfaces. Michael Czajkowski, Drexel University.1 The 3-step program to handling events Code that implements the listener class Code that implements the listener methods Code that registers the listener to a source public class MyClass implements ActionListener { … } component.addActionListener (instanceOfMyClass); public void actionPerformed (ActionEvent e) { … }

CS338: Graphical User Interfaces. Michael Czajkowski, Drexel University.1 Simple example (note: applet!) Beep when the user clicks the button import javax.swing.JApplet;import javax.swing.JButton; import java.awt.Toolkit;import java.awt.BorderLayout; import java.awt.event.ActionListener;import java.awt.event.ActionEvent; public class Beeper extends JApplet implements ActionListener { JButton button; public void actionPerformed(ActionEvent e) { Toolkit.getDefaultToolkit().beep(); } public void init() { button = new JButton("Click Me"); getContentPane().add(button, BorderLayout.CENTER); button.addActionListener(this); } step 1step 2step 3 Note: Don’t worry about the applet specifics, like init()…

CS338: Graphical User Interfaces. Michael Czajkowski, Drexel University.1 KeyEvent Another event class, like ActionClass Has many methods, including: char getKeyChar (); // returns character typed int getModifiers (); // returns typed modifiers (shift, ctrl, etc.)

CS338: Graphical User Interfaces. Michael Czajkowski, Drexel University.1 KeyListener Another interface, like ActionListener Includes several methods: public interface KeyListener { public void keyPressed (KeyEvent e); // press public void keyReleased (KeyEvent e); // release public void keyTyped (KeyEvent e); // press+release }

CS338: Graphical User Interfaces. Michael Czajkowski, Drexel University.1 “KeyTest” example MyKeyListener class public class MyKeyListener implements KeyListener { public void keyTyped (KeyEvent e) { char c = e.getKeyChar(); KeyTest.windowPrint (String.valueOf (c)); // Note: uses windowPrint from ProgramTemplate.java } public void keyPressed (KeyEvent e) { } public void keyReleased (KeyEvent e) { } } do we need these?

CS338: Graphical User Interfaces. Michael Czajkowski, Drexel University.1 “KeyTest” example KeyTest class public class KeyTest { … public static void main (String[] args) { … frame.addKeyListener (new MyKeyListener()); … }

CS338: Graphical User Interfaces. Michael Czajkowski, Drexel University.1 “KeyTest2” example The last example defined a full class for a new KeyListener in the usual way. It’s prim & proper, but gets bulky. Too many classes. You can internalize! A quicker way of saying the same thing frame.addKeyListener (new KeyListener() { public void keyTyped (KeyEvent e) { char c = e.getKeyChar(); windowPrint (String.valueOf (c)); } public void keyPressed(KeyEvent e) { } public void keyReleased(KeyEvent e) { } });

CS338: Graphical User Interfaces. Michael Czajkowski, Drexel University.1 Our old friend “ClickMe” public class ClickMe { … public static void main (String[] args) { initialize (); JButton button = new JButton ("Click Me!"); button.addActionListener (new ActionListener () { public void actionPerformed (ActionEvent e) { buttonAction(); } }); … > frame.addWindowListener(new WindowAdapter() { public void windowClosing (WindowEvent e) { System.exit(0); } }); … }

CS338: Graphical User Interfaces. Michael Czajkowski, Drexel University.1 WindowListener Another listener interface Includes lots of methods: public interface WindowListener { public void windowActivated(WindowEvent e); public void windowClosed(WindowEvent e); public void windowClosing(WindowEvent e); public void windowDeactivated(WindowEvent e); public void windowDeiconified(WindowEvent e); public void windowIconified(WindowEvent e); public void windowOpened(WindowEvent e); }

CS338: Graphical User Interfaces. Michael Czajkowski, Drexel University.1 WindowAdapter Implements the WindowListener interface with null (do-nothing) methods Do we need this? Do we want this? Why is this only for multi-method interfaces? public class WindowAdapter implements WindowsListener { public void windowActivated(WindowEvent e) { } public void windowClosed(WindowEvent e) { } public void windowClosing(WindowEvent e) { } public void windowDeactivated(WindowEvent e) { } public void windowDeiconified(WindowEvent e) { } public void windowIconified(WindowEvent e) { } public void windowOpened(WindowEvent e) { } }

CS338: Graphical User Interfaces. Michael Czajkowski, Drexel University.1 Adapters vs. Interfaces Why use an adapter? – less code, less verbosity – cleaner (?) Why use an interface? – forces you to think about each method (?) – new classes can implement many interfaces, but can only extend one class/adapter

CS338: Graphical User Interfaces. Michael Czajkowski, Drexel University.1 Adapters vs. Interfaces The first is fine; the second is not! public class MyListener implements KeyListener, WindowsListener { public void keyPressed (KeyEvent e); public void keyReleased (KeyEvent e); … public void windowActivated(WindowEvent e) { … } public void windowClosed(WindowEvent e) { … } … } public class MyListener extends KeyAdapter, WindowsAdapter { public void keyPressed (KeyEvent e); public void keyReleased (KeyEvent e); … public void windowActivated(WindowEvent e) { … } public void windowClosed(WindowEvent e) { … } … } There is no multiple inheritance in Java!