Quick intro to 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

Mouse Listeners We continue our examination of GUIs by looking at how to interact with the mouse –Just as Java creates Events when the user interacts with.
Graphic User Interfaces Layout Managers Event Handling.
Event Handling.
Event Handling Events and Listeners Timers and Animation.
For IST410 Students only Events-1 Event Handling.
CS3157 Java UI Recitation. Material Covered: Overview of AWT components, Event Handling, creating applets, and sample UI. Not covered in recitation: Drawing,
CSSE221: Software Dev. Honors Day 9 Announcements Announcements HW3 passed back, follow link from HW3 to its solution. HW3 passed back, follow link from.
CSE 331 Software Design & Implementation Dan Grossman Spring 2015 GUI Event-Driven Programming (Based on slides by Mike Ernst, Dan Grossman, David Notkin,
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.
F27SB2 Software Development 2 Lecture 4: Java GUIs 3.
Chapter 11 Java AWT Part I: Mouse Events (Optional) Lecture Slides to Accompany An Introduction to Computer Science Using Java (2nd Edition) by S.N. Kamin,
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,
By: Stag and Fish; Party Animals.  Radio buttons are a way of letting users choose only one option of a list.
Cs884(Prasad)java12AWT1 Abstract Windowing Toolkit Support for Graphical User Interface (Event-driven programming)
Java Event Handling CSIS 3701: Advanced Object Oriented Programming.
CSE 219 Patterns in Programming More Design Patterns.
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.
(c) University of Washington07b-1 CSC 143 Java Events, Event Handlers, and Threads Reading: Ch. 17.
 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.
More GUI CSCE 190 – Java Instructor: Joel Gompert Lecture 8 – July 28, 2004.
CS Lecture 00 Swing overview and introduction Lynda Thomas
Copyright © 2002, Systems and Computer Engineering, Carleton University b-Gui2.ppt * Object-Oriented Software Development Part 18-b Building.
EE2E1. JAVA Programming Lecture 6 Event handling and building user interfaces with Swing.
Design Patterns CSIS 3701: Advanced Object Oriented Programming.
Creating Windows. How can we use Java to create programs that use windows (GUI applications)? How can we use Java to create programs that use windows.
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.
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.
Java GUI. Overview The ”idea” The ”idea” The Components The Components Listeners Listeners The Program The Program Interface Eventlistener Interface Eventlistener.
GUI DYNAMICS Lecture 11 CS2110 – Fall GUI Statics and GUI Dynamics  Statics: what’s drawn on the screen  Components buttons, labels, lists, sliders,
Mouse Listeners Moving the mouse will also generate events like the Timer –To have your program respond, you must implement either or both of MouseListener.
Review_6 AWT, Swing, ActionListener, and Graphics.
CS Lecture 04 Mice Lynda Thomas
Lecture 18: Events; Cool Applets Yoni Fridman 7/30/01 7/30/01.
Peter Andreae Computer Science Victoria University of Wellington Copyright: Peter Andreae, Victoria University of Wellington Event-driven Input COMP 102.
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,
1 Event Handling – Lecture 4 Prepared by: Ahmad Ramin Rahimee Assistant Professor ICTI.
Mouse Events GUI. Types of Events  Below, are some of the many kinds of events, swing components generate. Act causing EventListener Type User clicks.
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,
Clicker quiz 11/5/13 CSE 1102 Fall A. a FlowLayout B. a BorderLayout C. a GridGrouping D. a JSplitPane E. a GridLayout Suppose we want to to lay.
5-1 GUIs and Events Rick Mercer. 5-2 Event-Driven Programming with Graphical user Interfaces  Most applications have graphical user interfaces to respond.
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.
Events and Event Handling
CompSci 230 S Programming Techniques
Web Design & Development Lecture 11
A First Look at GUI Applications
INFSY 547: WEB-Based Technologies
Computer Science 209 Graphics and GUIs.
Event loops 16-Jun-18.
CSE 331 Software Design and Implementation
Programming in Java Event Handling
GUI Programming III: Events
Event Driven Programming
CSE 331 Software Design and Implementation
Event loops.
Web Design & Development Lecture 12
Event loops 17-Jan-19.
Event loops 17-Jan-19.
Events, Event Handlers, and Threads
Event loops 8-Apr-19.
Tonga Institute of Higher Education
Event loops.
Event loops.
Event loops 19-Aug-19.
Presentation transcript:

Quick intro to Event Driven Programming Dan Fleck George Mason University

What is Event Driven Programming When you want to know if some external entity (generally a person) does something you have two options Polling - repeatedly ask if it has occurred Event based - tell the event generator to let you know when it has occurred

Example A Button exists on a form, you want to know when the Button is pressed: Tell the button you want to know: redButton.addActionListener(myObj); Tells “redButton” that whenever the button is pressed, let “myObj” know. myObj is listening for button presses.

Example When the button is pressed, the method from the ActionListener interface is called on myObj (which must implement the interface). Then myObj can do what it wants to do whenever the button is pressed

Listener Pattern Important Your code implements the xxxListener interface (requiring you to have specific methods in your class) Your code calls someObject.addXxxListener(this) to register yourself “this” as a listener SomeObject adds you into a list of listeners When the event of interest happens, all listeners have one of their specific xxxListener interface methods called with the XXXEvent object. Important

Code Example public class DoSomething implements MouseMotionListener { private JPanel jp; public DoSomething() { jp = new JPanel(); jp.addMouseMotionListener(this); } // Specified in the MouseMotionListener interface public void mouseDragged(MouseEvent e) { System.out.printf (“Mouse dragged to location (%d, %d) “,e.getX(), e.getY()); public void mouseMoved(MouseEvent e) { System.out.printf (“Mouse moved to location (%d, %d) “,e.getX(), e.getY());

Code Example JPanel Class … public void addMouseMotionListener(MouseMotionListener m) { mouseMotionListeners.add(m); } // Called when something happens with the mouse private void fireMouseMotionEvent() { MouseMotionEvent mEvent = new MouseMotionEvent(x, y, …..); // Notify the listeners! for (MouseMotionListener m : mouseMotionListeners) { if (dragged) m.mouseDragged(mEvent); else m.mouseMoved(mEvent); } // NOTE: This is NOT really the code, but it is essentially the idea that is used in the real JVM code.

That’s it! Many types of listeners and Events are present in Java Usually these are from the User Interface classes (Buttons, panels, sliders, checkboxes, etc…).