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.

Slides:



Advertisements
Similar presentations
Event Handling.
Advertisements

Liang,Introduction to Java Programming,revised by Dai-kaiyu 1 Chapter 12 Event-Driven Programming 找出画中真谛 — 保罗. 塞尚.
Jan Event Handling -1.1 Yangjun Chen Dept. Business Computing University of Winnipeg.
Events and the AWT The objectives of this chapter are: To understand the principles of the Java 1.1 event model To understand how the event model is used.
Event Handling. In this class we will cover: Basics of event handling The AWT event hierarchy Semantic and low-level events in the AWT.
OOP&M - theory lectures1 OOP&M – the sixth day “… there must be a secret out there …” - Spok -
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.
Lecture 5 Event Handling.
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.
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.
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.
Graphical User Interface Components: Part 1 Chapter 11.
1 Outline 1 Introduction 2 Overview of Swing Components 3 JLabel 4 Event Handling 5 TextFields 6 How Event Handling Works 7 JButton 8 JCheckBox and JRadioButton.
Graphic User Interface. Graphic User Interface (GUI) Most of us interact with computers using GUIs. GUIs are visual representations of the actions you.
1 Unit 5 GUI Aum Amriteshwaryai Namah. 2 Overview Shall learn how to reuse the graphics classes provided by Java for constructing Graphical User Interface.
COMP 321 Week 2. Outline Event-Driven Programming Events, Event Sources, Event Listeners Button and Timer Events Mouse Events, Adapters.
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. 2 GUIs are event driven –Generate events when user interacts with GUI e.g., moving mouse, pressing button, typing in text field, etc.
Universidad Nacional de Colombia Facultad de Ingeniería Departamento de Sistemas ertificación en AVA.
CMSC 341 Making Java GUIs Functional. 09/29/2007 CMSC 341 Events 2 More on Swing Great Swing demo at /demos/jfc/SwingSet2/SwingSet2Plugin.html.
Lesson 6 Programming Techniques Event Handling /EvH/ AUBG ICoSCIS Team Assoc. Prof. Stoyan Bonev March, , 2013 SWU, Blagoevgrad.
© Marty Hall, Larry Brown Web core programming 1 Handling Mouse and Keyboard Events.
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,
Event-Driven Programming CSCI 201L Jeffrey Miller, Ph.D. HTTP :// WWW - SCF. USC. EDU /~ CSCI 201 USC CSCI 201L.
Events (Chapter 11) Java Certification Study Group January 25, 1999 Mark Roth.
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,
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.
Mouse Events GUI. Types of Events  Below, are some of the many kinds of events, swing components generate. Act causing EventListener Type User clicks.
Sep 181 Example Program DemoTranslateEnglishGUI.java.
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.
EVENT HANDLING 10/8/2017.
Chapter 14 Event-Driven Programming
Welcome To java
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.
Aum Amriteshwaryai Namah
Handling User Events with Swing
Event Handling Chapter 2 Objectives
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
Unit I: Chapter 1 Event handling.
CHAPTER 2 Event Handling.
The Delegation Event Model
Web Design & Development Lecture 12
Chapter 16 Event-Driven Programming
Making Java GUIs Functional
Presentation transcript:

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.

Packages Java.util Java.awt Java.awt.event etc.

Two Event Handling Mechanisms original version of Java (1.0) modern versions of Java, beginning with version 1.1.

The Delegation Event Model A source generates an event and sends it to one or more listeners.

Source Listener events

Events -An event is an object that describes a state change in a source. -It can be generated as a consequence of a person interacting with the elements in a graphical user interface. Examples. Pressing a button, entering a character via the keyboard, selecting an item in a list, and clicking the mouse, timer expires, counter, h/w or s/w failure.

Event Sources A source is an object that generates an event. Source has three responsibilities: (1)It must provide methods that allow listener to register and unregister for notification about a specific type of event. (2)It must generate the event. (3)It must send the event to all the listeners.

Methods implemented by a source for listeners public void addTypeListener(TypeListener el) e.g. public void addActionListener(ActionListener al) public void addTypeListener(TypeListener el)throws java.util.TooManyListenersException public void removeTypeListener(TypeListener el) public void removeActionListener(ActionListener al)

Event Listeners A listener is an object that receives notifications when event is generated. It has three responsibilities: (1) It must register about specific events to get notifications.(with one or more sources.) (2)It must implement methods to receive and process these notifications. (methods defined in interface) (3)It must unregister itself if it is no longer wants to receive notifications.

Container s and Component s The job of a Container is to hold and display Component s Some common subclasses of Component are Button, Checkbox, Label, Scrollbar, TextField, and TextArea A Container is also a Component – This allows Containers to be nested Some Container subclasses are Panel (and Applet ), Window, and Frame

An Applet is Panel is a Container java.lang.Object | +----java.awt.Component | +----java.awt.Container | +----java.awt.Panel | +----java.applet.Applet

13 Example Container ( Applet ) Containers ( Panel s) Component ( Canvas ) Components ( Button s) Components ( Label s) Components ( TextField s)

Event Classes

EventObject contains two methods: getSource( ) and toString() general form : Object getSource( ) -The class AWTEvent, defined within the java.awt package which is a sub class of EventObject. -AWTEvent is a superclass of all AWT events

Event Classes

Event Listener Interfaces – Listeners are created by implementing one or more of the interfaces defined by the java.awt.event package. – When an event occurs, the event source invokes the appropriate method defined by the listener and provides an event object as its argument.

The ActionEvent Class Source : Button, List, Menu An ActionEvent is generated when a button is pressed, a list item is double-clicked,or a menu item is selected. Interface : ActionListener Method : void actionPerformed(ActionEvent ae)

The AdjustmentEvent Class Source : Scrollbar An AdjustmentEvent is generated by a scroll bar. Interface : AdjustmentListener Method: void adjustmentValueChanged (AdjustmentEvent ae)

The ComponentEvent Class A ComponentEvent is generated when the size, position, or visibility of a component is changed. Interface : ComponentListener COMPONENT_HIDDEN :The component was hidden. COMPONENT_MOVED: The component was moved. COMPONENT_RESIZED :The component was resized. COMPONENT_SHOWN: The component became visible.

Method : void componentResized(ComponentEvent ce) void componentMoved(ComponentEvent ce) void componentShown(ComponentEvent ce) void componentHidden(ComponentEvent ce)

The ContainerEvent Class A ContainerEvent is generated when a component is added to or removed from a container. Interface : ContainerListener COMPONENT_ADDED COMPONENT_REMOVED.

Method : void componentAdded(ContainerEvent ce) void componentRemoved(ContainerEvent ce)

The FocusEvent Class A FocusEvent is generated when a component gains or loses input focus. Interface : FocusListener FOCUS_GAINED FOCUS_LOST. Method : void focusLost(FocusEvent fe) void focusGained(FocusEvent fe)

The ItemEvent Class Source : checkbox, choice, list, menuitem, DESELECTED The user deselected an item. SELECTED The user selected an item. Interface : ItemListener Method : void itemStateChanged(ItemEvent ie)

The InputEvent Class InputEvent is a subclass ComponentEvent and is the super class for key events and mouse events.

The KeyEvent Class A KeyEvent is generated when keyboard input occurs. Interface : KeyListener KEY_PRESSED,KEY_RELEASED, and KEY_TYPED. Method : void keyPressed(KeyEvent ke) void keyReleased(KeyEvent ke) void keyTyped(KeyEvent ke)

VK_0 through VK_9 and VK_A through VK_Z 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

The MouseEvent Class Interface : MouseListener, MouseMotionListener 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.

Method : void mouseClicked(MouseEvent me) void mouseEntered(MouseEvent me) void mouseExited(MouseEvent me) void mousePressed(MouseEvent me) void mouseReleased(MouseEvent me) void mouseDragged(MouseEvent me) void mouseMoved(MouseEvent me)

The TextEvent Class Source : TextComponent These are generated by text fields and text areas when characters are entered by a user or program. Interface : TextListener TEXT_VALUE_CHANGED. Method : void textChanged(TextEvent te)

The WindowEvent Class 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 was deiconified. WINDOW_GAINED_FOCUS The window gained input focus. WINDOW_ICONIFIED The window was iconified. WINDOW_LOST_FOCUS The window lost input focus. WINDOW_OPENED The window was opened. WINDOW_STATE_CHANGED The state of the window changed. (Added by Java 2, version 1.4) Interface : WindowListener

Method : 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) void windowGainedFocus(WindowEvent we) void windowLostFocus(WindowEvent we)

Sources of Events

Syntax of AWT program (without component) Import pkgs; applet code public class Demo extends Applet implements AnyTypeListener { …. public void init() {…register the listener..} implement all methods of interface(listener) public void paint(Graphics g) //optional {…….} }

Handling Mouse Events To handle mouse events, you must implement the MouseListener and the MouseMotionListener interfaces.

// Demonstrate the mouse event handlers. import java.awt.*; import java.awt.event.*; import java.applet.*; /* */

public class MouseEvents extends Applet implements MouseListener, MouseMotionListener { String msg = ""; int mouseX = 0, mouseY = 0; // coordinates of mouse public void init() { addMouseListener(this); addMouseMotionListener(this); } // Handle mouse clicked. public void mouseClicked(MouseEvent me) { // save coordinates mouseX = 0; mouseY = 10; msg = "Mouse clicked."; repaint(); }

// Handle mouse entered. public void mouseEntered(MouseEvent me) { // save coordinates mouseX = 0; mouseY = 10; msg = "Mouse entered."; repaint(); } // Handle mouse exited. public void mouseExited(MouseEvent me) { // save coordinates mouseX = 0; mouseY = 10; msg = "Mouse exited."; repaint(); }

// Handle button pressed. public void mousePressed(MouseEvent me) { // save coordinates mouseX = me.getX(); mouseY = me.getY(); msg = "Down"; repaint(); } // Handle button released. public void mouseReleased(MouseEvent me) { // save coordinates mouseX = me.getX(); mouseY = me.getY(); msg = "Up"; repaint(); }

// Handle mouse dragged. public void mouseDragged(MouseEvent me) { // save coordinates mouseX = me.getX(); mouseY = me.getY(); msg = "*"; showStatus("Dragging mouse at " + mouseX + ", " + mouseY); repaint(); } // Handle mouse moved. public void mouseMoved(MouseEvent me) { // show status showStatus("Moving mouse at " + me.getX() + ", " + me.getY()); } // Display msg in applet window at current X,Y location. public void paint(Graphics g) { g.drawString(msg, mouseX, mouseY); }

Handling Keyboard Events // Demonstrate the key event handlers. import java.awt.*; import java.awt.event.*; import java.applet.*; /* */

public class SimpleKey extends Applet implements KeyListener { String msg = ""; int X = 10, Y = 20; // output coordinates public void init() { addKeyListener(this); } public void keyPressed(KeyEvent ke) { showStatus("Key Down"); } public void keyReleased(KeyEvent ke) { showStatus("Key Up"); }

public void keyTyped(KeyEvent ke) { msg += ke.getKeyChar(); repaint(); } // Display keystrokes. public void paint(Graphics g) { g.drawString(msg, X, Y); }

// Demonstrate some virutal key codes. import java.awt.*; import java.awt.event.*; import java.applet.*; /* */ public class KeyEvents extends Applet implements KeyListener { String msg = ""; int X = 10, Y = 20; // output coordinates public void init() { addKeyListener(this); }

public void keyPressed(KeyEvent ke) { showStatus("Key Down"); int key = ke.getKeyCode(); switch(key) { case KeyEvent.VK_F1: msg += " "; break; case KeyEvent.VK_F2: msg += " "; break; case KeyEvent.VK_F3: msg += " "; break; case KeyEvent.VK_PAGE_DOWN: msg += " "; break; case KeyEvent.VK_PAGE_UP: msg += " "; break;

case KeyEvent.VK_LEFT: msg += " "; break; case KeyEvent.VK_RIGHT: msg += " "; break; } repaint(); } public void keyReleased(KeyEvent ke) { showStatus("Key Up"); } public void keyTyped(KeyEvent ke) { msg += ke.getKeyChar(); repaint(); }

/ Display keystrokes. public void paint(Graphics g) { g.drawString(msg, X, Y); }

Adapter class 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.

Program for Adapter class

Inner class It is defined within the scope of a class. public class InnerDemo extends Applet { public void init() { …….addMouseListener(new MyMouseAdapter());… } Class MyMouseAdapter extends MouseAdapter { public void mousePressed(MouseEvent me) { …………….} }

Program for Inner Class

Anonymous Inner Classes An anonymous inner class is one that is not assigned a name public class Demo extends Applet { public void init() { …….addMouseListener(new MyMouseAdapter() { public void mousePressed(MouseEvent me) { …………….} } ); }

Program for Annonymous Inner class