CSI 3125, Preliminaries, page 1 Event Handling. CSI 3125, Preliminaries, page 2 Event Handling An Event Change in the state of an object is known as event.

Slides:



Advertisements
Similar presentations
Event Handling.
Advertisements

Liang,Introduction to Java Programming,revised by Dai-kaiyu 1 Chapter 12 Event-Driven Programming 找出画中真谛 — 保罗. 塞尚.
Event Handling. In this class we will cover: Basics of event handling The AWT event hierarchy Semantic and low-level events in the AWT.
Mouse Events and Keyboard Events
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.
Events ● Anything that happens in a GUI is an event. For example: – User clicks a button, presses return when typing text, or chooses a menu item ( ActionEvent.
For IST410 Students only Events-1 Event Handling.
Unit 111 Event-Driven Programming Listener or Event handler Example: Handling Button Events Example: Handling Mouse Events Example: Handling Keyboard Events.
Event-Driven Programming
Event Handling n Events: an event is an object that describes a state change in a source. n Event Sources: A source is an object that generates an event.
Event-Driven Programming
OOP Java1 Event Handling Overview Listeners, Adapters and Event Sources Inner classes Event Handling Details Applets and GUI Applications Event.
1 Gui Programming (Part I) Graphical User Interfaces (Part I) l Introduction to events. l A Brief history. l Event sources and listeners. l The delegation.
Intermediate Java1 An example that uses inner classes Week Four Continued.
1 lecture 12Lecture 13 Event Handling (cont.) Overview  Handling Window Events.  Event Adapters Revisited.  Introduction to Components and Containers.
Liang, Introduction to Java Programming, Ninth Edition, (c) 2013 Pearson Education, Inc. All rights reserved. 1 Chapter 16 Event-Driven Programming.
GUI Event Handling Nithya Raman. What is an Event? GUI components communicate with the rest of the applications through events. The source of an event.
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.
28-Aug-15 Air Force Institute of Technology Electrical and Computer Engineering Object-Oriented Programming Design Topic : Event Handling – GUI Part II.
Mouse Events. Handling Mouse Events Java provides two listener interfaces to handle mouse events: MouseListener;  MouseListener;  MouseMotionListener.
1 CSE 331 More Events (Mouse, Keyboard, Window, Focus, Change, Document...) slides created by Marty Stepp based on materials by M. Ernst, S. Reges, D.
More Event Handling Adapters Anonymous Listeners Pop menus Validating User Input.
Cs884(Prasad)java12AWT1 Abstract Windowing Toolkit Support for Graphical User Interface (Event-driven programming)
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.
Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rights reserved Chapter 14 Event-Driven Programming.
Event Handling Mohanraj S AP / IT Angel College of Engg & Tech.,
Graphic User Interface. Graphic User Interface (GUI) Most of us interact with computers using GUIs. GUIs are visual representations of the actions you.
COMP 321 Week 2. Outline Event-Driven Programming Events, Event Sources, Event Listeners Button and Timer Events Mouse Events, Adapters.
Liang, Introduction to Java Programming, Fifth Edition, (c) 2005 Pearson Education, Inc. All rights reserved Chapter 12 Event-Driven Programming.
Pravin Yannawar, DOCS, NMU Jalgaon. Basic Java : Event handling in AWT and Swing 2 Objectives of This Session Explain the Event handling mechanism & demonstrate.
Event Handling. Event Driven Programming Flow of programs is determined by events. The Operating system recognizes events and passes them to the particular.
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.
Anonymous Classes An anonymous class is a local class that does not have a name. An anonymous class allows an object to be created using an expression.
Lesson 6 Programming Techniques Event Handling /EvH/ AUBG ICoSCIS Team Assoc. Prof. Stoyan Bonev March, , 2013 SWU, Blagoevgrad.
Object Oriented Programming.  Interface  Event Handling.
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.
GUI DYNAMICS Lecture 11 CS2110 – Fall GUI Statics and GUI Dynamics  Statics: what’s drawn on the screen  Components buttons, labels, lists, sliders,
Events in JAVA Needed for Applets and GUI based programs. Applet uses GUI to interact with user. Most of the events are generated by Mouse, Keyboard.
Event-Driven Programming CSCI 201L Jeffrey Miller, Ph.D. HTTP :// WWW - SCF. USC. EDU /~ CSCI 201 USC CSCI 201L.
Event-Driven Programming F Procedural programming is executed in procedural order. F In event-driven programming, code is executed upon activation of events.
1 Event Handling – Lecture 4 Prepared by: Ahmad Ramin Rahimee Assistant Professor ICTI.
2/17/2016 DEPT OF CSE 1 EVENT HANDLING. 2/17/2016 DEPT OF CSE 2 Event handling is at the core of successful applet programming There are several types.
EVENT-DRIVEN PROGRAMMING Subject:T0934 / Multimedia Programming Foundation Session:2 Tahun:2009 Versi:1/0.
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.
Event Listeners ActionListener –button,list AdjustmentListener-scroll bar ComponentListener-when component hidden…. ContainerListener-comp added or removed.
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,
Mouse Events GUI. Types of Events  Below, are some of the many kinds of events, swing components generate. Act causing EventListener Type User clicks.
TENTH LECTURE Event and listener. Events and Listeners An event can be defined as a type of signal to the program that something has happened. The event.
Dept. of CSIE, National University of Tainan 10/21/2012 Responding to User Input.
Prepared by: Dr. Abdallah Mohamed, AOU-KW Unit7: Event-driven programming 1.
GUI Programming in Java Hao Jiang Boston College April, 2009.
Events and Event Handling
Chapter 14 Event-Driven Programming
Programming in Java, 2e Sachin Malhotra Saurabh Choudhary.
CHAPTER Reacting to the user.
Chapter 12 Event-Driven Programming
Programming in Java Sachin Malhotra, Chairperson, PGDM-IT, IMS Ghaziabad Saurabh Chaudhary, Dean, Academics, IMS Ghaziabad.
Event Handling Chapter 2 Objectives
Java Events. Java Events Important definitions Overridden method Class vs. abstract class vs. interface Event Handling Definition Main components Windows.
Programming in Java Event Handling
GUI Event Handling Nithya Raman.
GUI Programming III: Events
CSE Software Engineering Fall 1999 Updated by J. Brown
Event-driven programming for GUI
Chapter 16 Event-Driven Programming
Programming Graphical User Interface (GUI)
Presentation transcript:

CSI 3125, Preliminaries, page 1 Event Handling

CSI 3125, Preliminaries, page 2 Event Handling An Event Change in the state of an object is known as event i.e. Event describes the change in state of source. Events are generated as result of user interaction with the graphical user interface components. For example, clicking on a button, moving the mouse, entering a character through keyboard,selecting an item from list, scrolling the page are the activities that causes an event to happen.

CSI 3125, Preliminaries, page 3 Event Handling Types of Event The events can be broadly classified into two categories: Foreground Events - Those events which require the direct interaction of user. A person interacting with the graphical components in Graphical User Interface. For example, clicking on a button, moving the mouse, entering a character through keyboard, selecting an item from list, scrolling the page etc. Background Events - Those events that require the interaction of end user are known as background events. Operating system interrupts, hardware or software failure, timer expires, an operation completion are the example of background events.

CSI 3125, Preliminaries, page 4 Event Handling Event Handling is the mechanism that controls the event and decides what should happen if an event occurs. This mechanism have the code which is known as event handler that is executed when an event occurs.

CSI 3125, Preliminaries, page 5 Event Handling 3 Components of event handling Event :- Change of state of an object Source :- Object that generate an event Listener :- Object that listen to the event A listener get notified when an event occurs A source generates an Event and send it to one or more listeners registered with the source. Once event is received by the listener, they process the event and then return. Events are supported by a number of Java packages like Java.util, java.awt,java.awt.event

CSI 3125, Preliminaries, page 6 Event Handling Steps involved in event handling The User clicks the button and the event is generated. Now the object of concerned event class is created automatically and information about the source and the event get populated with in same object. Event object is forwarded to the method of registered listener class. The method is now get executed and returns.

CSI 3125, Preliminaries, page 7 Event Handling Following steps are required to perform event handling: Implement the Listener interface and overrides its methods Register the component with the Listener

CSI 3125, Preliminaries, page 8 Event Handling Some of the event classes are ActionEvent  Generated when a Button is pressed, or menu is selected, or doubled clicked in a list item… AdjustmentEvent  Generated when a scroll bar is manipulated ComponenetEvent  when a component is moved, or hiddenor resize.. FocusEvent  when a component gain or lost fosus ItemEvent  when check box or list item is clicked KeyEvent  when an input received through keyboard MouseEvent  when mouse clicked, dragged, doubled clicked…. WindowEvent  when a window activated, closed, deactivated….

CSI 3125, Preliminaries, page 9 Mouse Event Handling prog The interface MouseListener contains 5 methods, should override these meths First register the Listener addMouseListener(this); in init() public void mouseClicked(MouseEvent m)  when button clicked public void mouseExited(MouseEvent m)  when exited from window public void mouseEntered(MouseEvent m)  when entered in the window public void mouseReleased(MouseEvent m)  when button released public void mousePressed(MouseEvent m)--> when button pressed Methods of MouseEvent class getX()  return x co-ordinate getY()  return y co-ordinate getClickCount()  return mouse click count getButton()  return the mouse Button

CSI 3125, Preliminaries, page 10 Mouse Event Handling prog import java.applet.*; import java.awt.*; import java.awt.event.*; /* */ public class mouse extends Applet implements MouseListener { int x,y; String str; public void init() {addMouseListener(this);} public void mouseClicked(MouseEvent m) {str="Clicked";x=m.getX();y=m.get Y(); repaint();} public void mouseExited(MouseEvent m) {str="Exited";x=m.getX();y=m.getY (); repaint();} public void mouseEntered(MouseEvent m) {str="Entered";x=m.getX();y= m.getY(); repaint();} public void mouseReleased(MouseEvent m) {str="Released";x=m.getX();y =m.getY(); repaint();} public void mousePressed(MouseEvent m) {str="Pressed";x=m.getX();y= m.getY(); repaint();} public void paint(Graphics g) {g.drawString(str+" coodinates "+x+" "+y,50,50);} }

CSI 3125, Preliminaries, page 11 Mouse motion Event Handling prog This interface defines 2 methods public void mouseDragged(MouseEvent m)  when dragged public void mouseMoved(MouseEvent m)  when moved

CSI 3125, Preliminaries, page 12 Mouse motion Event Handling prog import java.applet.*; import java.awt.*; import java.awt.event.*; /* */ public class mouse1 extends Applet implements MouseMotionListener { int x,y,z; String str; public void init() {addMouseMotionListener(this);} public void mouseDragged(MouseEventm) {str="dragged";x=m.getX();y=m.getY(); repaint();} public void mouseMoved(MouseEvent m) {str="move";x=m.getX();y=m.getY(); repaint();} public void paint(Graphics g) {g.drawString(str+" coodinates "+x+" "+y,50,50);} }

CSI 3125, Preliminaries, page 13 Key Event Handling prog This interface defines 3 methods should override the methods Public void keyPressed(KeyEvent k)  when a key is pressed Public void keyReleased(KeyEvent k)  when a key is released Public void keyTyped(KeyEvent k)  when character typed When a user pressed and released a key, 3 events are generated

CSI 3125, Preliminaries, page 14 Key Event Handling import java.applet.*; import java.awt.*; import java.awt.event.*; /* */ public class key extends Applet implements KeyListener { int x,y,z,a,b; String str; public void init() {addKeyListener(this);} public void keyTyped(KeyEvent m) {str="Typed";repaint();} public void keyReleased(KeyEvent m) {str="Released";repaint();} public void keyPressed(KeyEvent m) {str="Pressed";repaint();} public void paint(Graphics g) {g.drawString("Key "+str,50,50);} }

CSI 3125, Preliminaries, page 15 Scrollbar Event Handling import java.awt.*; import java.awt.event.*; import java.applet.*; /* */ public class M extends Applet implements AdjustmentListener { Scrollbar slider; int sliderValue = 0; public void init() { slider=new Scrollbar(Scrollbar.HORIZONTAL,0, 1, 0, 101); add(slider); slider.addAdjustmentListener(this); } public void paint(Graphics g) { slider.setLocation(20,30); slider.setSize(150,20); g.setFont(new Font("TimesRoman", Font.BOLD, 20)); g.drawString("Current value is " + sliderValue, 50,100); } public void adjustmentValueChanged(Adjustme ntEvent e) { sliderValue = slider.getValue(); repaint(); }

CSI 3125, Preliminaries, page 16 The WindowListener Interface This interface defines seven methods. 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)

CSI 3125, Preliminaries, page 17 The WindowListener Interface void windowActivated(WindowEvent e) Invoked when the Window is set to be the active Window. void windowClosed(WindowEvent e) Invoked when a window has been closed as the result of calling dispose on the window. void windowClosing(WindowEvent e) Invoked when the user attempts to close the window from the window's system menu. void windowDeactivated(WindowEvent e) Invoked when a Window is no longer the active Window. void windowDeiconified(WindowEvent e) Invoked when a window is changed from a minimized to a normal state. void windowIconified(WindowEvent e) Invoked when a window is changed from a normal to a minimized state. void windowOpened(WindowEvent e) Invoked the first time a window is made visible.

CSI 3125, Preliminaries, page 18 WindowListener Handling import java.awt.*; import java.awt.event.*; public class w1 extends Frame implements WindowListener { public static void main(String qrg[]) {w1 f=new w1(); } public w1() { addWindowListener(this); Frame f=new Frame(); setTitle("POPO Frame"); setSize(500,500); setVisible(true); } public void windowActivated(WindowEvent we){System.out.println("Activated");} public void windowClosed(WindowEvent we){System.out.println("Closed");} public void windowClosing(WindowEvent we){System.out.println("Closing");} public void windowDeactivated(WindowEvent we){System.out.println("Deactivated");} public void windowDeiconified(WindowEvent we){System.out.println("Deicnified");} public void windowIconified(WindowEvent we){System.out.println("Iconified");} public void windowOpened(WindowEvent we){System.out.println("Opened");} }

CSI 3125, Preliminaries, page 19 Event Handling