Advanced Java Programming (17625) Event Handling. 20 Marks

Similar documents
The AWT Event Model 9

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

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

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

Interacción con GUIs

Handling Mouse and Keyboard Events

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

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

APPLETS Applet Architecture readline( )

Event Driven Programming

Dr. Hikmat A. M. AbdelJaber

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

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

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

COMPSCI 230. Software Design and Construction. Swing

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

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

GUI Programming: Swing and Event Handling

GUI DYNAMICS Lecture July 26 CS2110 Summer 2011

GUI Event Handlers (Part II)

GUI Event Handlers (Part I)

GUI Software Architecture

SD Module-1 Advanced JAVA

Chapter 1 GUI Applications

SD Module-1 Advanced JAVA. Assignment No. 4

GUI in Java TalentHome Solutions

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

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

3 Events and Listeners

BM214E Object Oriented Programming Lecture 13

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

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

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

Išdėstymai. Lekt. dr. Pijus Kasparaitis m. m. pavasario semestras.

Java AWT Windows, Text, & Graphics

Computer Programming Java AWT Lab 18

Programming Mobile Devices J2SE GUI

PESIT Bangalore South Campus

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

Graphical User Interfaces 2

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

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

กล ม API ท ใช. Programming Graphical User Interface (GUI) Containers and Components 22/05/60

Lecture 19 GUI Events

CS 251 Intermediate Programming GUIs: Event Listeners

Unit 7: Event driven programming

Course: CMPT 101/104 E.100 Thursday, November 23, 2000

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

CSE 331 Software Design & Implementation

Objekt-orienteeritud programmeerimine MTAT (6 EAP) 7. Loeng. H e l l e H e i n h e l l e. h e i e e

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

Outline. More on the Swing API Graphics: double buffering and timers Model - View - Controller paradigm Applets

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

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

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

CSE 331 Software Design and Implementation. Lecture 19 GUI Events

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

Graphical User Interfaces 2

Java: Graphical User Interfaces (GUI)

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

Graphical User Interfaces 2

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

CSE 8B Intro to CS: Java

user-friendly and easy to use.

Programming Languages and Techniques (CIS120)

Cheng, CSE870. More Frameworks. Overview. Recap on OOP. Acknowledgements:

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

CS 180 Problem Solving and Object Oriented Programming Fall 2011

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

Overloading Example. Overloading. When to Overload. Overloading Example (cont'd) (class Point continued.)

G51PRG: Introduction to Programming Second semester Applets and graphics

ECE 462 Object-Oriented Programming using C++ and Java. Key Inputs in Java Games

Graphical Interfaces

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

COSC 123 Computer Creativity. Graphics and Events. Dr. Ramon Lawrence University of British Columbia Okanagan

Graphical Interfaces

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

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

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

Computer Science 210: Data Structures. Intro to Java Graphics

Programming Languages and Techniques (CIS120)

Advanced Java Programming

Administrivia. CSSS Movie Night: Zombieland & Iron Man Date: Thurs., Mar 11 Time: 6 10 pm Location: DMP 310 Free pop & popcorn for every attendee!

14.2 Java s New Nimbus Look-and-Feel 551 Sample GUI: The SwingSet3 Demo Application As an example of a GUI, consider Fig. 14.1, which shows the SwingS

Java for Interfaces and Networks (DT3010, HT10)

Programming Languages and Techniques (CIS120e)

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

Systems Programming Graphical User Interfaces

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

Java Event Handling -- 1

Maharashtra State Board of Technical Education

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

Event Handling in JavaFX

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

CS11 Java. Fall Lecture 3

FORMAS DE IMPLEMENTAR LOS OYENTES. A).- El oyente en una clase independiente con el constructor con un argumento y usando el método getactioncommand.

CS2110. GUIS: Listening to Events

Java Mouse Actions. C&G criteria: 5.2.1, 5.4.1, 5.4.2,

AWT KEYEVENT CLASS. static int KEY_FIRST --The first number in the range of ids used for key events.

Transcription:

Advanced Java Programming (17625) Event Handling 20 Marks

Specific Objectives To write event driven programs using the delegation event model. To write programs using adapter classes & the inner classes. 2

The Delegation Event Model Modern approach to handling events is based on the delegation event model. Defines standard and consistent mechanisms to generate and process event s. Source generates an event and sends it to one or more listeners. 3

The Delegation Event Model : Event Event is an object that describes a state change in a source. Some action we have to performed when it is generated. Generated when user is interacted with components. Example: Pressing Button, Selecting item from list etc. It can be also generated when timer expires, counter value exceeds, software and hardware failure etc. 4

Event Source Source is an object that generates an event. This occurs when the internal state of that object changes. Sources may generate more than one type of event. Sources must be register listener so that listener will receive notifications. For Register/add: public void addtypelistener(typelistener el) For remove: public void removetypelistener(typelistener el) 5

Event Listener Listener is an object that is notified when an event occurs. Two requirements: It must have been registered with one or more sources to receive notifications. It must implement methods to receive and process these notifications. 6

Event Classes Event Classes are core of Java s event handling mechanism. EventObject is the root of the Java event class hierarchy which is present in java.util. It is the superclass for all events. Constructor: EventObject(Object src). EventObject class has defines two methods: Object getsource( ) : method returns the source of the event. String tostring( ) : returns string equivalent of the event. 7

Event Classes : AWTEvent AWTEvent is defined in java.awt package. It is a subclass of EventObject. It is the superclass of all AWT-based events. Summarize: EventObject is a superclass of all events. AWTEvent is a superclass of all AWT events that are handled by the delegation event model. 8

Summary Event Source the class which broadcasts the events Event Listeners the classes which receive notifications of events Event Object the class object which describes the event. 9

Event Classes : Diff classes ActionEvent ComponentEvent ContainerEvent FocusEvent ItemEvent KeyEvent MouseEvent TextEvent WindowEvent 10

ActionEvent Generated when a button is pressed, a list item is double-clicked, or a menu item is selected. ActionEvent class defines four integer constants that can be used to identify any modifiers associated with an action event: ALT_MASK, (8) CTRL_MASK, (2) META_MASK, (4) SHIFT_MASK. (1) In addition, an integer constant, ACTION_PERFORMED (1001), which can be used to identify action events 11

ActionEvent Constructors: ActionEvent(Object src, int type, String cmd) ActionEvent(Object src, int type, String cmd, int modifiers) ActionEvent(Object src, int type, String cmd, long when, int modifiers) src: object which generate event type: type of event cmd: Command string modifiers: which modifier key when: when the event occurred 12

ActionEvent getactioncommand() used to get command name. int getmodifiers() used to get modifier key. long getwhen( ) used to get when event generated. 13

ComponentEvent class A ComponentEvent is generated when the size, position, or visibility of a component is changed. There are four types of component events COMPONENT_HIDDEN The component was hidden. COMPONENT_MOVED The component was moved. COMPONENT_RESIZED The component was resized. COMPONENT_SHOWN The component became visible. Constructor: ComponentEvent(Component src, int type) 14

ContainerEvent class ContainerEvent is generated when a component is added to or removed from a container. Two Constants defined COMPONENT_ADDED and COMPONENT_REMOVED Subclass of ComponentEvent Class Constructor: ContainerEvent(Component src, int type, Component comp) 15

FocusEvent class FocusEvent is generated when a component gains or loses input focus. Two constants defined: FOCUS_GAINED and FOCUS_LOST. Constructors: FocusEvent(Component src, int type) FocusEvent(Component src, int type, boolean temporaryflag) Focus Event(Component src, int type, boolean temporaryflag, Component other) istemporary( ) method indicates if this focus change is temporary. 16

ItemEvent Class ItemEvent is generated when a check box or a list item is clicked or when a checkable menu item is selected or deselected. Item events: DESELECTED The user deselected an item. SELECTED The user selected an item. ITEM_STATE_CHANGED that signifies a change of state. Constructor: ItemEvent(ItemSelectable src, int type, Object entry, int state) 17

KeyEvent Class KeyEvent is generated when keyboard input occurs. There are three types of key events: KEY_PRESSED, KEY_RELEASED, and KEY_TYPED Constructor: KeyEvent(Component src, int type, long when, int modifiers, int code) KeyEvent(Component src, int type, long when, int modifiers, int code, char ch) 18

KeyEvent Class There are many other integer constants that are defined by KeyEvent. VK_0 through VK_9 and VK_A through VK_Z define the ASCII equivalents of the numbers and letters. VK_ENTER VK_ESCAPE VK_CANCEL VK_UP VK_DOWN VK_LEFT VK_RIGHT VK_PAGE_DOWN VK_PAGE_UP VK_SHIFT VK_ALT VK_CONTROL 19

MouseEvent Class Eight types of mouse events. The MouseEvent class defines the following integer constants MOUSE_CLICKED The user clicked the mouse. MOUSE_DRAGGED The user dragged the mouse. MOUSE_ENTERED The mouse entered a component. MOUSE_EXITED The mouse exited from a component. MOUSE_MOVED The mouse moved. MOUSE_PRESSED The mouse was pressed. MOUSE_RELEASED The mouse was released. MOUSE_WHEEL The mouse wheel was moved 20

MouseEvent Class MouseEvent is a subclass of InputEvent. Constructor: MouseEvent(Component src, int type, long when, int modifiers, int x, int y, int clicks, boolean triggerspopup) 21

TextEvent Class These are generated by text fields and text areas when characters are entered by a user or program. TextEvent defines the integer constant TEXT_VALUE_CHANGED. Constructor: TextEvent(Object src, int type) 22

WindowEvent Class There are ten types of window events. WindowEvent class defines integer constants: WINDOW_ACTIVATED The window was activated. WINDOW_CLOSED The window has been closed. WINDOW_CLOSING The user requested that the window be closed. WINDOW_DEACTIVATED The window was deactivated. WINDOW_DEICONIFIED The window deiconified (min => Normal). WINDOW_GAINED_FOCUS The window gained input focus. WINDOW_ICONIFIED The window was iconified(normal=>min) WINDOW_LOST_FOCUS The window lost input focus. WINDOW_OPENED The window was opened. WINDOW_STATE_CHANGED The state of the window changed. 23

WindowEvent Class WindowEvent is a subclass of ComponentEvent. WindowEvent(Window src, int type, Window other) WindowEvent(Window src, int type, int fromstate, int tostate) WindowEvent(Window src, int type, Window other, int fromstate, int tostate) 24

Adapter Class An adapter class provides an empty implementation of all methods in an event listener interface. Adapter classes are useful when you want to receive and process only some of the events that are handled by a particular event listener interface. Example: 25

Adapter Class : Different Classes ComponentAdapter ComponentListener ContainerAdapter ContainerListener FocusAdapter FocusListener KeyAdapter KeyListener MouseAdapter MouseListener MouseMotionAdapter MouseMotionListener WindowAdapter WindowListener 26

Inner Class Inner class is class which defined in another class. In inner classes, the Adapter class will defined in same class. No need of passing reference of object as it is in same scope. Ex. 27

Anonymous Inner Class An anonymous inner class is one that is not assigned a name. Ex. 28

Event Listeners Interfaces Event Delegation Model has two parts: Sources and Listeners. When event generated, then event source invoked appropriate method defined by interface. 29

Action Listener Interface Defines one method to receive action events. void actionperformed(actionevent ae) 30

ComponentListener Interface Defines four methods to recognize when a component is hidden, moved, resized, or shown. void componentresized(componentevent ce) void componentmoved(componentevent ce) void componentshown(componentevent ce) void componenthidden(componentevent ce) 31

ContainerListener Interface Defines two methods to recognize when a component is added to or removed from a container. void componentadded(containerevent ce) void componentremoved(containerevent ce) 32

FocusListener Interface Defines two methods to recognize when a component gains or loses keyboard focus. void focusgained(focusevent fe) void focuslost(focusevent fe) 33

ItemListener Interface Defines one method to recognize when the state of an item changes. void itemstatechanged(itemevent ie) 34

KeyListener Interface Defines three methods to recognize when a key is pressed, released, or typed. void keypressed(keyevent ke) void keyreleased(keyevent ke) void keytyped(keyevent ke) 35

MouseListener Interface Defines five methods to recognize when the mouse is clicked, enters a component, exits a component, is pressed, or is released. void mouseclicked(mouseevent me) void mouseentered(mouseevent me) void mouseexited(mouseevent me) void mousepressed(mouseevent me) void mousereleased(mouseevent me) 36

MouseMotionListener Interface Defines two methods to recognize when the mouse is dragged or moved. void mousedragged(mouseevent me) void mousemoved(mouseevent me) 37

TextListener Interface Defines one method to recognize when a text value changes. void textvaluechanged(textevent te) 38

WindowFocusListener Interface Defines two methods to recognize when a window gains or loses input focus void windowgainedfocus(windowevent we) void windowlostfocus(windowevent we) 39

WindowListener Interface Defines seven methods to recognize: void windowactivated(windowevent we) void windowclosed(windowevent we) void windowclosing(windowevent we) void windowdeactivated(windowevent we) void windowdeiconified(windowevent we) void windowiconified(windowevent we) void windowopened(windowevent we) 40