Lecture 9.5 Event Delegation.

Slides:



Advertisements
Similar presentations
Unit 3 Graphical User Interface (GUI) Dr. Magdi AMER.
Advertisements

Liang,Introduction to Java Programming,revised by Dai-kaiyu 1 Chapter 12 Event-Driven Programming 找出画中真谛 — 保罗. 塞尚.
Event Handling Events and Listeners Timers and Animation.
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.
1 Lecturte 14Lecture 7 Applications of Graphics Overview  Conversions Between Applications and Applets  Handling Mouse Events  Handling Keyboard Events.
Event-Driven Programming
Unit 12 Object-oriented programming: Event-driven programming for GUI Jin Sa.
Intermediate Java1 An example that uses inner classes Week Four Continued.
Abstract Classes and Interfaces. Abstract methods You can declare an object without defining it: Person p; Similarly, you can declare a method without.
Liang, Introduction to Java Programming, Ninth Edition, (c) 2013 Pearson Education, Inc. All rights reserved. 1 Chapter 16 Event-Driven Programming.
1 Event-Driven Programming Just like designing GUIs, you also will probably not write Java code like the program examples given in these notes. You will.
Chapter 6: Graphical User Interface (GUI) and Object-Oriented Design (OOD) J ava P rogramming: Program Design Including Data Structures Program Design.
ACM/JETT Workshop - August 4-5, ExceptionHandling and User Interfaces (Event Delegation, Inner classes) using Swing.
Java Programming: From Problem Analysis to Program Design, Second Edition1  Learn about basic GUI components.  Explore how the GUI components JFrame,
CSE115: Introduction to Computer Science I Dr. Carl Alphonce 343 Davis Hall
Chapter 12 1 TOPIC 13B l Buttons and Action Listeners Window Interfaces Using Swing Objects.
COMP 321 Week 2. Outline Event-Driven Programming Events, Event Sources, Event Listeners Button and Timer Events Mouse Events, Adapters.
Lecture 8.5 Animating with EventTimer. © 2006 Pearson Addison-Wesley. All rights reserved A Crash Course in the Use of Timed Events What kinds of.
Java Applet Basics (2). The Body Mass Index Calculator.
Java GUI. Graphical User Interface (GUI) a list a button a text field a label combo box checkbox.
Java Inner Classes. Interfaces interface KeyListener { public void keyPressed(KeyEvent e); public void keyReleased(KeyEvent e); public void keyTyped(KeyEvent.
Lesson 6 Programming Techniques Event Handling /EvH/ AUBG ICoSCIS Team Assoc. Prof. Stoyan Bonev March, , 2013 SWU, Blagoevgrad.
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.
Index Event Handling Events Event Source Event Listener Event Classes Action Event Class Adjustment event Class Event Source Event Listener Interface Using.
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.
Practical Session 10: Animation. Animation kinds There are three basic kinds of animation: 1. Moving object 2. Changing pictures 3. Combination of 1 and.
Creating User Interfaces Event-Driven Programming.
Event-Driven Programming CSCI 201L Jeffrey Miller, Ph.D. HTTP :// WWW - SCF. USC. EDU /~ CSCI 201 USC CSCI 201L.
עקרונות תכנות מונחה עצמים תרגול 7: אנימציה. בשבוע שעבר  paint  Graphics  repaint.
Nested Classes and Event Handling Chapter 10. Overview We explain how to write Java code that responds to events. The Timer class can be used to respond.
Event-Driven Programming F Procedural programming is executed in procedural order. F In event-driven programming, code is executed upon activation of events.
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,
Chapter 10 - Writing Graphical User Interfaces1 Chapter 10 Writing Graphical User Interfaces.
Chapter 6 Graphical User Interface (GUI) and Object-Oriented Design (OOD)
Sep 181 Example Program DemoTranslateEnglishGUI.java.
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.
Lecture 8.5 Animating with EventTimer. © 2006 Pearson Addison-Wesley. All rights reserved A Crash Course in the Use of Timed Events What kinds of.
Event Driven (Asynchronous) Programming. Event handling in Unity Subclass a class that contains event handling methods, and then override those methods.
Dept. of CSIE, National University of Tainan 10/21/2012 Responding to User Input.
CompSci Inheritance & Interfaces. CompSci Inheritance & Interfaces The Plan  Motivate Inheritance  Motivate Interfaces  Examples  Continue.
©TheMcGraw-Hill Companies, Inc. Permission required for reproduction or display. Chapter 7 Event-Driven Programming and Basic GUI Objects.
Lecture 15 Basic GUI programming
Lecture 15.1 Event Delegation.
Modular Event Handling
Events and Event Handling
Chapter 14 Event-Driven Programming
CompSci 230 S Programming Techniques
Animating with EventTimer
Programming in Java, 2e Sachin Malhotra Saurabh Choudhary.
CHAPTER Reacting to the user.
Chapter 12 Event-Driven Programming
Abstract Classes and Interfaces
Keyboard Events keyboard acceleration TextFields
Programming in Java Sachin Malhotra, Chairperson, PGDM-IT, IMS Ghaziabad Saurabh Chaudhary, Dean, Academics, IMS Ghaziabad.
Web Design & Development Lecture 11
Appendix I GUI Components and Event Handling
Processing Timer Events
Java Programming: From Problem Analysis to Program Design,
Programming in Java Event Handling
Miscellaneous Topics #6: Polygons GUI Components and Event Handlers
Event-driven programming for GUI
Introduction to Event Handling
Chapter 16 Event-Driven Programming
Java Inner Classes.
Events, Event Handlers, and Threads
Chapter 2 Reference Types.
Presentation transcript:

Lecture 9.5 Event Delegation

Event Handling How to Delegate events Java event handling is based on interfaces, known as listeners, and a concept known a delegation. How to Delegate events (1) Create some class that ... • implements the listener for the appropriate kind of event • overrides the proper event handler method (2) Your code must ... (a) instantiate some object capable of generating events (eventGenObject). (b) instantiate an object (delegateObject) belonging to the class from (1). (c) call eventGenObject.addEventType Listener(delegateObject); © 2006 Pearson Addison-Wesley. All rights reserved

Example home of many event classes and listeners import java.awt.event.*; public class BtnHandler implements ActionListener { public BtnHandler() {} public void actionPerformed(ActionEvent e) { System.out.println( “Another useless event message” ); } import javax.swing.JButton; ... JButton myButton; myButton = new JButton( “Button Label” ); myButton.setBounds(10, 10, 100, 20); win.add( myButton, 0 ); BtnHandler buttonHandler = new BtnHandler(); myButton.addActionListener( buttonHandler ); © 2006 Pearson Addison-Wesley. All rights reserved

EventTimer public abstract class EventTimer extends Timer import java.awt.event.*; import javax.swing.Timer; public abstract class EventTimer extends Timer implements ActionListener { public EventTimer( int m ) { super(m, null); addActionListener( this ); } public abstract void actionPerformed(ActionEvent e); The “null” in the super call is another location where the delegate can be included. In other words, for this particular class it would work to use super(m,this) and not call addActionListener. © 2006 Pearson Addison-Wesley. All rights reserved

Keystroke Events Note that keystroke events are “generated” by window objects, such as JFrames. «interface» java.awt.event.KeyListener «event handler» + void keyPressed(java.awt.Event.KeyEvent e) + void keyReleased(java.awt.Event.KeyEvent e) + void keyTyped(java.awt.Event.KeyEvent e) java.awt.event.KeyEvent «query» + char getKeyChar() + int getKeyCode() getKeyChare returns the character associated with printable characters. getKeyCode returns a unique integer for EVERY keystroke. © 2006 Pearson Addison-Wesley. All rights reserved

JFrameWithKeys public class JFrameWithKeys extends JFrame import java.awt.event.*; import javax.swing.JFrame; public class JFrameWithKeys extends JFrame implements KeyListener { public JFrameWithKeys(String s) { super(s); addKeyListener( this ); } public void keyPressed(KeyEvent e) { System.out.println( e.getKeyCode() ); public void keyReleased(KeyEvent e) { } public void keyTyped(KeyEvent e) { } The “null” in the super call is another location where the delegate can be included. In other words, for this particular class it would work to use super(m,this) and not call addActionListener. © 2006 Pearson Addison-Wesley. All rights reserved