Event Driven Programming

Similar documents
Module 4 Multi threaded Programming, Event Handling. OOC 4 th Sem, B Div Prof. Mouna M. Naravani

CS 251 Intermediate Programming GUIs: Event Listeners

Advanced Java Programming (17625) Event Handling. 20 Marks

The AWT Event Model 9

11/7/12. Discussion of Roulette Assignment. Objectives. Compiler s Names of Classes. GUI Review. Window Events

COMPSCI 230. Software Design and Construction. Swing

Method Of Key Event Key Listener must implement three methods, keypressed(), keyreleased() & keytyped(). 1) keypressed() : will run whenever a key is

Dr. Hikmat A. M. AbdelJaber

Java GUI Design: the Basics

Lecture 19 GUI Events

GUI Event Handling 11. GUI Event Handling. Objectives. What is an Event? Hierarchical Model (JDK1.0) Delegation Model (JDK1.1)

CSE 331 Software Design & Implementation

10/16/2008. CSG 170 Round 5. Prof. Timothy Bickmore. User Analysis Task Analysis (6) Problem Scenarios (3)

Module 4 Multi threaded Programming, Event Handling. OOC 4 th Sem, B Div Prof. Mouna M. Naravani

CSE 331 Software Design and Implementation. Lecture 19 GUI Events

GUI Program Organization. Sequential vs. Event-driven Programming. Sequential Programming. Outline

GUI Event Handlers (Part I)

Example Programs. COSC 3461 User Interfaces. GUI Program Organization. Outline. DemoHelloWorld.java DemoHelloWorld2.java DemoSwing.

UNIT-3 : MULTI THREADED PROGRAMMING, EVENT HANDLING. A Multithreaded program contains two or more parts that can run concurrently.

Control Flow: Overview CSE3461. An Example of Sequential Control. Control Flow: Revisited. Control Flow Paradigms: Reacting to the User

Java: Graphical User Interfaces (GUI)

Assignment No 2. Year: Dept.: ComputerTech. Sanjivani Rural Education Society s Sanjivani KBP Polytechnic, Kopargaon

PROGRAMMING DESIGN USING JAVA (ITT 303) Unit 7

Which of the following syntax used to attach an input stream to console?

GUI DYNAMICS Lecture July 26 CS2110 Summer 2011

Graphical User Interfaces 2

MIT AITI Swing Event Model Lecture 17

(listener)... MouseListener, ActionLister. (adapter)... MouseAdapter, ActionAdapter. java.awt AWT Abstract Window Toolkit GUI

Java Graphical User Interfaces AWT (Abstract Window Toolkit) & Swing

GUI Programming: Swing and Event Handling

PIC 20A Anonymous classes, Lambda Expressions, and Functional Programming

Marcin Luckner Warsaw University of Technology Faculty of Mathematics and Information Science

Java & Graphical User Interface II. Wang Yang wyang AT njnet.edu.cn

Graphical User Interfaces 2

GUI in Java TalentHome Solutions

Graphical User Interfaces 2

GUI Software Architecture

Inheritance. One class inherits from another if it describes a specialized subset of objects Terminology:

PESIT Bangalore South Campus

Event Binding. Different Approaches Global Hooks. 2.5 Event Binding 1

GUI 4.1 GUI GUI MouseTest.java import javax.swing.*; import java.awt.*; import java.awt.event.*; /* 1 */

Events. Dispatch, event-to-code binding. Review: Events Defined 1/17/2014. occurrence.

OBJECT ORIENTED PROGRAMMING. Course 8 Loredana STANCIU Room B613

PIC 20A GUI with swing

CS 201 Advanced Object-Oriented Programming Lab 10 - Recursion Due: April 21/22, 11:30 PM

public void mouseexited (MouseEvent e) setminute(getminute()+increment); 11.2 public void mouseclicked (MouseEvent e) int x = e.getx(), y = e.gety();

Unit 7: Event driven programming

SD Module-1 Advanced JAVA

SD Module-1 Advanced JAVA. Assignment No. 4

Event-driven Programming: GUIs

An Animated Help System for Java Applets

CSE1720. Objectives for this class meeting 2/10/2014. Cover 2D Graphics topic: displaying images. Timer class, a basic ActionListener

AP Computer Science Unit 13. Still More Graphics and Animation.

Class 16: The Swing Event Model

Programmierpraktikum

CSE1720 Lecture 08; Week 05 Lecture 09 Second level Third level Fourth level Fifth level

Interacción con GUIs

Lab 10: Inheritance (I)

Event Handling in JavaFX

Solution register itself

Hanley s Survival Guide for Visual Applications with NetBeans 2.0 Last Updated: 5/20/2015 TABLE OF CONTENTS

Virtualians.ning.pk. 2 - Java program code is compiled into form called 1. Machine code 2. native Code 3. Byte Code (From Lectuer # 2) 4.

CSE 143. Event-driven Programming and Graphical User Interfaces (GUIs) with Swing/AWT

Java Programming. Events and Listeners

Previously, we have seen GUI components, their relationships, containers, layout managers. Now we will see how to paint graphics on GUI components

CS11 Java. Fall Lecture 3

Graphical User Interface (GUI)

Introduction to concurrency and GUIs

Graphical User Interface (GUI)

DEMYSTIFYING PROGRAMMING: CHAPTER FOUR

Advanced Java Unit 6: Review of Graphics and Events

CS 106A, Lecture 14 Events and Instance Variables

Chapter 6: Graphical User Interfaces

CSE115 / CSE503 Introduction to Computer Science I. Dr. Carl Alphonce 343 Davis Hall Office hours:

Java for Interfaces and Networks (DT3010, HT10)

Java Foundations John Lewis Peter DePasquale Joe Chase Third Edition

CPS122 Lecture: Graphical User Interfaces and Event-Driven Programming

Outline. Topic 9: Swing. GUIs Up to now: line-by-line programs: computer displays text user types text AWT. A. Basics

I/O Framework and Case Study. CS151 Chris Pollett Nov. 2, 2005.

G51PGP Programming Paradigms. Lecture 008 Inner classes, anonymous classes, Swing worker thread

CS211 GUI Dynamics. Announcements. Motivation/Overview. Example Revisted

Graphics. Lecture 18 COP 3252 Summer June 6, 2017

Bean Communication. COMP434B Software Design. Bean Communication. Bean Communication. Bean Communication. Bean Communication

GUI Components: Part 1

The JFrame Class Frame Windows GRAPHICAL USER INTERFACES. Five steps to displaying a frame: 1) Construct an object of the JFrame class

CS2110. GUIS: Listening to Events

CS 112 Programming 2. Lecture 14. Event-Driven Programming & Animations (1) Chapter 15 Event-Driven Programming and Animations

Come & Join Us at VUSTUDENTS.net

EVENTS, EVENT SOURCES AND LISTENERS

IPM 12/13 P4 Handling Events in AWT

3 Combining Widgets to create Graphical User Interfaces

Java Event Handling -- 1

Handling Mouse and Keyboard Events

CSEN401 Computer Programming Lab. Topics: Graphical User Interface Window Interfaces using Swing

CS2110. GUIS: Listening to Events. Anonymous functions. Anonymous functions. Anonymous functions. Checkers.java. mainbox

CS2110. GUIS: Listening to Events

CS2110. GUIS: Listening to Events Also anonymous classes versus Java 8 functions. Anonymous functions. Anonymous functions. Anonymous functions

2010 가을학기부산대학교정보컴퓨터공학부 OVERVIEW OF GUI PROGRAMMING

GUI (Graphic User Interface) Programming. Part 2 (Chapter 8) Chapter Goals. Events, Event Sources, and Event Listeners. Listeners

GUI Event Handlers (Part II)

Transcription:

Event Driven Programming 1. Objectives... 2 2. Definitions... 2 3. Event-Driven Style of Programming... 2 4. Event Polling Model... 3 5. Java's Event Delegation Model... 5 6. How to Implement an Event Handler?... 7 A. Bellaachia Page: 1

1. Objectives The user is in charge instead of the program: He or she can perform tasks in any order. 2. Definitions Events can be triggered by human interaction or alternatively a software or hardware process causing an event to be triggered. An event is a hardware condition that is signaled Common events are from input devices: mouse events - mouse up, mouse down, mouse move keyboard events - key up, key down network events - packet arrival window events - window resized, window dragged, focus lost Special hardware detects and signals these events. Each event has additional information. For example a mouse movement event has an x and y coordinate. 3. Event-Driven Style of Programming In event-driven programming, there is essentially no normal flow-of-control! Only the event handlers exist. A. Bellaachia Page: 2

Flow-of-control in the program is entirely driven by responding to events. Program is driven by responding to events Two main models: Polling: o Has the event happened yet? o Are we there yet? Are we there yet? Etc. Delegating: o Here is what I want you to do when this event occurs. 4. Event Polling Model How does it work? Loop: Polling tends to push all event-handling code into one location (inside the big loop The resulting interactions within the big loop tend to be complex. In addition, polling requires a program to sit in a loop, consuming CPU cycles, while waiting for the user to do something -- a serious waste of a valuable resource. The Event Loop pseudo-code: loop wait for an event to arrive get the event details A. Bellaachia Page: 3

do something based on the event until a stop event occurs Examples: Windows and Palm OS The Basic Palm Event Loop The basic Palm event loop looks like this: EventType event; do { EvtGetEvent( &event, evtwaitforever ); // dispatch the event here } while( event.etype!= appstopevent ); // until told to stop Where EvtGetEvent is a Palm OS API: It returns the next event for the application by copying it into the EventType structure passed in as the first parameter. The second parameter is a timeout value, which tells the operating system how long the application wants to wait before an event occurs: evtwaitforever constant: Wait indefinitely Number of ticks: Pass in the number of ticks (hundredths of a second) to wait. A. Bellaachia Page: 4

5. Java's Event Delegation Model Supersedes Java's original containment and inheritance based event handling. This model is more efficient since not all events are handled by your program. Three kinds of things (with various corresponding Java classes): 1. Events: Representation of things that happen, derived from the superclass EventObject. They occur in response to an action by the user within the Graphical User Interface (GUI). When an event occurs, the source of the event notifies listeners. 2. Event sources: Objects that can generate events It only notifies listeners that have been registered to receive the event Examples: Event Source How triggered Event Generated Button Button pressed Action events Checkbox Checkbox selected or deselected Item events A. Bellaachia Page: 5

Multicasting of the event: When an event occurs, the source notifies all the listeners that have registered for that event. The notification includes the details about the event, for example, the name of the source that generated the event, the key that was pressed or the location where the mouse was clicked. Example of events: Event Classes Description ActionEvent Button pressed, list item selected, or item menu selected TextEvent Value or text field or text area changed. KeyEvent Key is pressed or released or typed MouseEvent Mouse clicked, pressed, released, dragged, moved, enters, or exits a compment. FocusEvent Component gains or loses focus(keyboard). 3. Event listeners (handlers): Are responsible to recognize and process events. Objects that want to respond to events. A listener is an object that is notified by the source, if registered, when an event occurs. Register their interest in certain events with event sources. Listeners usually sit back and waits for an event to happen. A. Bellaachia Page: 6

6. How to Implement an Event Handler? Every event handler requires three bits of code: 1. In the declaration for the event handler class, code that specifies that the class either implements a listener interface or extends a class that implements a listener interface. For example: public class MyButtonHandler implements ActionListener { // class members... // constructor } 2. Code that registers an instance of the event handler class as a listener upon one or more components. For example: public class MyButtonHandler implements ActionListener { // class members... // constructor //somecomponent.addactionlistener(instanceofmyclass); button.addactionlistener(this); } 3. Code that implements the methods in the listener interface. For example: A. Bellaachia Page: 7

public class MyButtonHandler implements ActionListener { // class members... // constructor } //somecomponent.addactionlistener(instanceofmyclass); button.addactionlistener(this); public void actionperformed(actionevent e) { // handle the event // code that reacts to the action... } C l i c k Button ActionEvent Action Listener When the user clicks a button, the button's action listeners are notified. A. Bellaachia Page: 8

Some Event Classes KeyEvent: for keyboard input MouseEvent: for all sorts of mouse events: press, release, move, drag,... ActionEvent: for GUI actions like clicking on a button, selecting a menu item,...... Some Event Listener Interfaces MouseListener: Methods: void mouseclicked( MouseEvent me) void mouseentered( MouseEvent me) void mouseexited( MouseEvent me) void mousepressed( MouseEvent me) void mousereleased( MouseEvent me) Registered with some event source by calling: MouseMotionListener Methods: void addmouselistener( MouseListener ml ) void mousedragged( MouseEvent me) void mousemoved( MouseEvent me) Registered with some event source by calling: A. Bellaachia Page: 9

KeyListener void addmousemotionlistener( MouseMotionListener mml ) Methods: void keypressed( KeyEvent ke) void keyreleased( KeyEvent ke) void keytyped( KeyEvent ke) Registered with some event source by calling void addkeylistener( KeyListener kl ) ActionListener Methods: void actionperformed( ActionEvent ae) Registered with some event source by calling: void addactionlistener( ActionListener al ) A. Bellaachia Page: 10