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

Similar documents
The AWT Event Model 9

Advanced Java Programming (17625) Event Handling. 20 Marks

Dr. Hikmat A. M. AbdelJaber

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

Event Driven Programming

Handling Mouse and Keyboard Events

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

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

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

GUI Software Architecture

JAVA. Object Oriented Programming with. M.T. Somashekara D.S.Guru K.S. Manjunatha

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

COMPSCI 230. Software Design and Construction. Swing

Interacción con GUIs

Java: Graphical User Interfaces (GUI)

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

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

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

GUI Programming: Swing and Event Handling

Chapter 1 GUI Applications

BM214E Object Oriented Programming Lecture 13

Java Event Handling -- 1

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

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

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

GUI Event Handlers (Part I)

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

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

CS 251 Intermediate Programming GUIs: Event Listeners

SD Module-1 Advanced JAVA

SD Module-1 Advanced JAVA. Assignment No. 4

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

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

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

Packages: Putting Classes Together

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

Java GUI Design: the Basics

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

An Animated Help System for Java Applets

Java for Interfaces and Networks (DT3010, HT10)

CSE 331 Software Design & Implementation

CSIS 10A Assignment 14 SOLUTIONS

OBJECT ORIENTED PROGRAMMING. Java GUI part 1 Loredana STANCIU Room B616

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

Unit 7: Event driven programming

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

Lecture 19 GUI Events

Lecture 5: Java Graphics

Advanced Java Unit 6: Review of Graphics and Events

CS11 Java. Fall Lecture 3

Programmierpraktikum

Lecture 3: Java Graphics & Events

GUI DYNAMICS Lecture July 26 CS2110 Summer 2011

Graphical interfaces & event-driven programming

Systems Programming Graphical User Interfaces

CSE 8B Intro to CS: Java

Programming Mobile Devices J2SE GUI

PESIT Bangalore South Campus

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

GUI in Java TalentHome Solutions

Programming Languages 2nd edition Tucker and Noonan"

OBJECT ORIENTED PROGRAMMING. Course 8 Loredana STANCIU Room B613

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

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

CSE 331 Software Design and Implementation. Lecture 19 GUI Events

Jonathan Aldrich Charlie Garrod

CS 106A, Lecture 14 Events and Instance Variables

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

Java AWT Windows, Text, & Graphics

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

2. (True/False) All methods in an interface must be declared public.

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.

DATABASE AUTOMATION USING VBA (ADVANCED MICROSOFT ACCESS, X405.6)

GUI Design. Overview of Part 1 of the Course. Overview of Java GUI Programming

AP CS Unit 12: Drawing and Mouse Events

CS 106A, Lecture 14 Events and Instance Variables

Index SELF-STUDY. Symbols

Applet which displays a simulated trackball in the upper half of its window.

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

7. Program Frameworks

Görünt Görün ülerin ölçeklenmesi ve yerle y ştirilmesi ş

Come & Join Us at VUSTUDENTS.net

Chapter 13 - Graphical User Interface Components: Part 1

Programming Languages and Techniques (CIS120e)

Introduction to GUIs. Principles of Software Construction: Objects, Design, and Concurrency. Jonathan Aldrich and Charlie Garrod Fall 2014

Lecture 28. Exceptions and Inner Classes. Goals. We are going to talk in more detail about two advanced Java features:

Advanced Java Programming

PIC 20A GUI with swing

More Swing. CS180 Recitation 12/(04,05)/08

Graphical User Interfaces 2

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

Watch the following for more announcements

Contents. 18 Algorithms Case study: network search The search algorithm Case study: automatic language recognition 57

The AWT Component Library 10

What Is an Event? Some event handler. ActionEvent. actionperformed(actionevent e) { }

Introduction to the JAVA UI classes Advanced HCI IAT351

Swing from A to Z Some Simple Components. Preface

Java Foundations John Lewis Peter DePasquale Joe Chase Third Edition

Charlie Garrod Michael Hilton

! #"$&%')(&%%*#+, #-!(&. /0'1 #"2. awt34 5#617

Transcription:

Objectives Write code to handle events that occur in a GUI 11 GUI Event Handling Describe the concept of adapter classes, including how and when to use them Determine the user action that originated the event from the event object details Create the appropriate interface and event handler methods for a variety of event types 356 357 What is an Event? Hierarchical Model (JDK1.0) Events - Objects that describe what happened Is based on containment Event sources - The generator of an event Event handlers - A method that receives an event object, deciphers it, and processes the user's interaction action() lostfocus() mouseexit() gotfocus() mousedown() mousemove() keydown() mousedrag() mouseup() keyup() mouseenter() Frame Panel Button Action event 358 359 Advantages It is simple and well suited to an object-oriented programming environment. Disadvantages An event can only be handled by the component from which it originated or by one of the containers of the originating component. In order to handle events, you must either subclass the component that receives the event or create a handleevent() method at the base container. Delegation Model (JDK1.1) Events are sent to the component from which the event originated, but it is up to each component to propagate the event to one or more registered classes called listener. Listeners contain event handlers that receive and process the event. In this way, the event handler can be in an object separate from the component. Listeners are classes that implement the EventListener interface. Events are objects that are reported only to registered listeners. Every event has corresponding listener interface that mandates which methods must be defined in a class suited to receiving that type of event. The class that implements the interface defines those methods, and can be registered as a listener. Events from components that have no registered listeners are not propagated. 360 361 1

Delegation Model Client objects (handlers) register with a GUI component they want to observe. Event Categories Java.awt awt.event GUI components only trigger the handlers for the type of event that has occurred Most components can trigger more than one type of event Distributes the work among multiple classes java.util.eventobject java.awt.awtevent InputEvent 362 363 Event Class Listener Interface ActionListener AdjustmentListener ComponentListener ContainerListener FocusListener ItemListener KeyListener Listener Methods actionperformed() adjustmentvaluechanged() componenthidden() componentmoved() componentresized() componentshown() componentadded() componentremoved() focusgained() focuslost() itemstatechanged() keypressed() keyreleased() keytyped() MouseMotionEvent MouseListener MouseMotionListener TextListener WindowListener mouseclicked() mouseentered() mouseexited() mousepressed() mousereleased() mousedragged() mousemoved() textvaluechanged() windowactivated() windowclosed() windowclosing() windowdeactivated() windowdeiconified() windowiconified() windowopened() 364 365 Component Button Checkbox CheckboxMenuItem Choice Component Events Generated Meaning User clicked on the button Component moved, resized, hidden, or shown Component gained or lost focus User pressed or released a key User pressed or released mouse button, mouse entered or exited component, or user moved or dragged mouse. Note: has two corresponding listeners, MouseListener and MouseMotion Listener. Container List MenuItem Scrollbar TextComponent TextField Window Component added to or removed from container User double-clicked on list item User selected a menu item User moved the scrollbar User changed text User finished editing text Window opened, closed, iconified, deiconified, or close requested 366 367 2

Example public class TwoListener implements MouseMotionListener, MouseListener public TwoListener() f = new Frame("Two listeners example") ; 368 f.addmousemotionlistener(this) ; f.addmouselistener(this) ; // These are MouseMotionListener events public void mousedragged( e) String s = "Mouse dragging: X= " + e.getx() + "Y= " + e.gety() ; 369 public void mouseentered( e) String s = "The mouse entered" ; public void mouseexited( e) String s = "The mouse has left the building" ; // Unused MouseMotionListener method // All methods of a listener must be present in the // class even if they are not used public void mousemoved( e) // Unused MouseListener methods public void mousepressed( e) public void mouseclicked( e) public void mousereleased( e) public static void main(string[] args) TwoListener two = new TwoListener() ; two.launchframe(); 370 371 Multiple Listeners Multiple listeners cause unrelated parts of a program to react to the same event. The handlers of all registered listeners are called when the event occurs 372 373 3

Event Adapters Event Handling Using Anonymous Classes The listener classes that you define can extend adapter classes and override only the methods that you need. You can include an entire class definition within the scope of an expression. Example: public class MouseClickHandler extends MouseAdapter public void mouseclicked( e) // do stuff with the mouse click... This approach defines what is called an anonymous inner class and creates the instance all at once. For example: next slide 374 375 public class TestAnonymous public TestAnonymous() f = new Frame("Anonymous class example") ; f.addmousemotionlistener(new MouseMotionAdapter() public void mousedragged( e) String s = "Mouse dragging: X= " + e.getx() + "Y= " + e.gety() ; ) ; f.addmouselistener( new MouseClickHandler(tf) ) ; public static void main(string[] args) TestAnonymous obj = new TestAnonymous() ; obj.launchframe(); 376 377 Event Handling Using Inner Classes public class MouseClickHandler extends MouseAdapter private TextField tf ; public static int count = 0 ; public MouseClickHandler(TextField tf) this.tf = tf ; public void mouseclicked( e) count++; String s = "Mouse has been clicked " + count + " times so far." ; You can implement event handlers as inner class. This allows access to the private data of the outer class. For example: next slide 378 379 4

public class TestInner public TestInner() f = new Frame("Inner classes example") ; f.addmousemotionlistener(new MyMouseMotionListener()) ; f.addmouselistener(new MouseClickHandler(tf)) ; class MyMouseMotionListener extends MouseMotionAdapter public void mousedragged( e ) String s = "Mouse dragging: X= " + e.getx() + "Y= " + e.gety() ; public static void main(string[] args) TestInner obj = new TestInner() ; obj.launchframe(); 380 381 5