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.

Slides:



Advertisements
Similar presentations
Event Handling.
Advertisements

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.
Mouse Events and Keyboard Events
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.
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
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.
Event Handling. In this class we will cover: Keyboard Events Mouse Events Focus Events Action Interface Multicasting.
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.
Io package as Java’s basic I/O system continue’d.
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.
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.
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.
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.
Omer Boyaci.  GUIs are event driven.  When the user interacts with a GUI component, the interaction—known as an event—drives the program to perform.
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.
Interactive Programs Java API. Terminology Event—an action or occurrence, not part of a program, detected by the program. Events can be Event—an action.
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.
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.
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.
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.
Jaeki Song ISQS6337 JAVA Lecture 10 Applets. Jaeki Song ISQS6337 JAVA Applet Applets run within the Web browser environment –Applets bring dynamic interaction.
UQC117S2 Graphics Programming Lecture 2 Event Handling Program as a sequence of instructions Event -driven program Need to detect the event and carry out.
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.
EVENT HANDLING 10/8/2017.
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
Unit 5 Applets: Applet class, Applet structure, An example of Applet, Applet life Cycle, Event Delagation Model, Java.awt.even description, Sources of.
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.
The Delegation Event Model
Chapter 16 Event-Driven Programming
Making Java GUIs Functional
Presentation transcript:

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 of events The most commonly handled events are those generated by the mouse, the keyboard, and various controls, such as a push button Events are supported by the java.awt.event package

2/17/2016 DEPT OF CSE 3 The Delegation Event Model Defines standard and consistent mechanisms to generate and process events A source generates an event and sends it to one or more listeners The listener simply waits until it receives an event Once received, the listener processes the event and then returns

2/17/2016 DEPT OF CSE 4 The advantage of this design is that the application logic that processes events is cleanly separated from the user interface logic that generates those events In the delegation event model, listeners must register with a source in order to receive an event notification

2/17/2016 DEPT OF CSE 5 Events In the delegation model, 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 Some of the activities that cause events to be generated are pressing a button, entering a character via the keyboard, selecting an item in a list, and clicking the mouse

2/17/2016 DEPT OF CSE 6 Event Sources A source is an object that generates an event Sources may generate more than one type of event A source must register listeners in order for the listeners to receive notifications about a specific type of event Each type of event has its own registration method. The general form: public void addTypeListener(TypeListener el) Type is the name of the event and el is a reference to the event listener

2/17/2016 DEPT OF CSE 7 A source must also provide a method that allows a listener to unregister an interest in a specific type of event The general form of such a method is this: public void removeTypeListener(TypeListener el) Here, Type is the name of the event and el is a reference to the event listener

2/17/2016 DEPT OF CSE 8 Event Listeners A listener is an object that is notified when an event occurs It has two major requirements First, it must have been registered with one or more sources to receive notifications about specific types of events Second, it must implement methods to receive and process these notifications

2/17/2016 DEPT OF CSE 9 Event Classes At the root of the Java event class hierarchy is EventObject, which is in java.util It is the superclass for all events. Its one constructor is shown here: EventObject(Object src) Here, src is the object that generates this event

2/17/2016 DEPT OF CSE 10 EventObject contains two methods: getSource( ) and toString( ) The getSource( ) method returns the source of the event Object getSource() toString( ) returns the string equivalent of the event

2/17/2016 DEPT OF CSE 11 The class AWTEvent, defined within the java.awt package, is a subclass of EventObject Its getID( ) method can be used to determine the type of the event int getID()

2/17/2016 DEPT OF CSE 12 Main Event Classes in java.awt.event

2/17/2016 DEPT OF CSE 13

2/17/2016 DEPT OF CSE 14 The ActionEvent Class An ActionEvent is generated when a button is pressed, a list item is double-clicked, or a menu item is selected The ActionEvent class defines four integer constants that can be used to identify any modifiers associated with an action event: ALT_MASK, CTRL_MASK, META_MASK, and SHIFT_MASK In addition, there is an integer constant, ACTION_PERFORMED, which can be used to identify action events

2/17/2016 DEPT OF CSE 15 ActionEvent has these three 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)

2/17/2016 DEPT OF CSE 16 Here, src is a reference to the object that generated this event The type of the event is specified by type, and its command string is cmd The argument modifiers indicates which modifier keys (ALT, CTRL, META, and/or SHIFT) were pressed when the event was generated The when parameter specifies when the event occurred

2/17/2016 DEPT OF CSE 17 You can obtain the command name for the invoking ActionEvent object by using the getActionCommand( ) method, shown here: String getActionCommand( ) For example, when a button is pressed, an action event is generated that has a command name equal to the label on that button

2/17/2016 DEPT OF CSE 18 The AdjustmentEvent Class An AdjustmentEvent is generated by a scroll bar The AdjustmentEvent class defines integer constants that can be used to identify them

2/17/2016 DEPT OF CSE 19

2/17/2016 DEPT OF CSE 20 In addition, there is an integer constant, ADJUSTMENT_VALUE_CHANGED, that indicates that a change has occurred The constructor: AdjustmentEvent(Adjustable src, int id, int type, int data) src is a reference to the object that generated this event The id equals ADJUSTMENT_VALUE_CHANGED The type of the event is specified by type, and its associated data is data

2/17/2016 DEPT OF CSE 21 The getAdjustable( ) method returns the object that generated the event The type of the adjustment event may be obtained by the getAdjustmentType( ) method

2/17/2016 DEPT OF CSE 22 The ComponentEvent Class A ComponentEvent is generated when the size, position, or visibility of a component is changed There are four types of component events The ComponentEvent class defines integer constants that can be used to identify them

2/17/2016 DEPT OF CSE 23

2/17/2016 DEPT OF CSE 24 ComponentEvent(Component src, int type) src is a reference to the object that generated this event The type of the event is specified by type The getComponent( ) method returns the component that generated the event

2/17/2016 DEPT OF CSE 25 The ContainerEvent Class A ContainerEvent is generated when a component is added to or removed from a container The ContainerEvent class defines int constants that can be used to identify them: COMPONENT_ADDED and COMPONENT_REMOVED

2/17/2016 DEPT OF CSE 26 ContainerEvent is a subclass of ComponentEvent and has this constructor: ContainerEvent(Component src, int type, Component comp) src is a reference to the container that generated this event The type of the event is specified by type and the component that has been added to or removed from the container is comp You can obtain a reference to the container that generated this event by using the getContainer( ) method

2/17/2016 DEPT OF CSE 27 The FocusEvent Class A FocusEvent is generated when a component gains or loses input focus These events are identified by the integer constants FOCUS_GAINED and FOCUS_LOST FocusEvent is a subclass of ComponentEvent and has these constructors: FocusEvent(Component src, int type) FocusEvent(Component src, int type, boolean temporaryFlag) Focus Event(Component src, int type, boolean temporaryFlag, Component other)

2/17/2016 DEPT OF CSE 28 src is a reference to the component that generated this event The type of the event is specified by type The argument temporaryFlag is set to true if the focus event is temporary; Otherwise, it is set to false If a FOCUS_GAINED event occurred, other will refer to the component that lost focus Conversely, if a FOCUS_LOST event occurred, other will refer to the component that gains focus

2/17/2016 DEPT OF CSE 29 The InputEvent Class The abstract class InputEvent is a subclass of ComponentEvent and is the superclass for component input events Its subclasses are KeyEvent and MouseEvent boolean isAltDown( ) boolean isAltGraphDown( ) boolean isControlDown( ) boolean isMetaDown( ) boolean isShiftDown( )

2/17/2016 DEPT OF CSE 30 The ItemEvent Class An ItemEvent is generated when a check box or a list item is clicked or when a checkable menu item is selected or deselected There are two types of item events, which are identified by the following integer constants: DESELECTED The user deselected an item. SELECTED The user selected an item.

2/17/2016 DEPT OF CSE 31 ItemEvent has this constructor: ItemEvent(ItemSelectable src, int type, Object entry, int state) src is a reference to the component that generated this event The type of the event is specified by type The specific item that generated the item event is passed in entry The current state of that item is in state

2/17/2016 DEPT OF CSE 32 The KeyEvent Class A KeyEvent is generated when keyboard input occurs There are three types of key events, which are identified by these integer constants: KEY_PRESSED, KEY_RELEASED, and KEY_TYPED The first two events are generated when any key is pressed or released The last event occurs only when a character is generated For example, pressing the SHIFT key does not generate a character

2/17/2016 DEPT OF CSE 33 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

2/17/2016 DEPT OF CSE 34 KeyEvent is a subclass of InputEvent. Here are two of its constructors: KeyEvent(Component src, int type, long when, int modifiers, int code) KeyEvent(Component src, int type, long when, int modifiers, int code, char ch) src is a reference to the component that generated the event The type of the event is specified by type The system time at which the key was pressed is passed in when The modifiers argument indicates which modifiers were pressed when this key event occurred The virtual key code, such as VK_UP, VK_A, and so forth, is passed in code

2/17/2016 DEPT OF CSE 35 The character equivalent (if one exists) is passed in ch If no valid character exists, then ch contains CHAR_UNDEFINED For KEY_TYPED events, code will contain VK_UNDEFINED

2/17/2016 DEPT OF CSE 36 The MouseEvent Class There are eight types of mouse events MouseEvent is a subclass of InputEvent. The MouseEvent class defines the following integer constants that can be used to identify them:

2/17/2016 DEPT OF CSE 37

2/17/2016 DEPT OF CSE 38 MouseEvent(Component src, int type, long when, int modifiers, int x, int y, int clicks, boolean triggersPopup) The modifiers argument indicates which modifiers were pressed when a mouse event occurred The coordinates of the mouse are passed in x and y. The click count is passed in clicks The triggersPopup flag indicates if this event causes a pop-up menu to appear on this platform

2/17/2016 DEPT OF CSE 39 The TextEvent Class Instances of this class describe text events 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 The one constructor for this class is shown here: TextEvent(Object src, int type) src is a reference to the object that generated this event, type of the event is specified by type

2/17/2016 DEPT OF CSE 40 The WindowEvent Class The WindowEvent class defines integer constants that can be used to identify them

2/17/2016 DEPT OF CSE 41 WindowEvent is a subclass of ComponentEvent. It defines several constructors.The first is WindowEvent(Window src, int type) src is a reference to the object that generated this event The type of the event is type

2/17/2016 DEPT OF CSE 42 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)

2/17/2016 DEPT OF CSE 43 other specifies the opposite window when a focus event occurs The fromState specifies the prior state of the window and toState specifies the new state that the window will have when a window state change occurs

2/17/2016 DEPT OF CSE 44 Sources of Events

2/17/2016 DEPT OF CSE 45 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

2/17/2016 DEPT OF CSE 46

2/17/2016 DEPT OF CSE 47

2/17/2016 DEPT OF CSE 48 The ActionListener Interface This interface defines the actionPerformed( ) method that is invoked when an action event occurs. Its general form is shown here: void actionPerformed(ActionEvent ae)

2/17/2016 DEPT OF CSE 49 The FocusListener Interface This interface defines two methods When a component obtains keyboard focus, focusGained( ) is invoked When a component loses keyboard focus, focusLost( ) is called Their general forms are shown here: void focusGained(FocusEvent fe) void focusLost(FocusEvent fe)

2/17/2016 DEPT OF CSE 50 The KeyListener Interface This interface defines three methods The keyPressed( ) and keyReleased( ) methods are invoked when a key is pressed and released, respectively The keyTyped( ) method is invoked when a character has been entered

2/17/2016 DEPT OF CSE 51 if a user presses and releases the A key, three events are generated in sequence: key pressed, typed, and released If a user presses and releases the HOME key, two key events are generated in sequence: key pressed and released The general forms of these methods are shown here: void keyPressed(KeyEvent ke) void keyReleased(KeyEvent ke) void keyTyped(KeyEvent ke)

2/17/2016 DEPT OF CSE 52 The MouseListener Interface This interface defines five methods void mouseClicked(MouseEvent me) void mouseEntered(MouseEvent me) void mouseExited(MouseEvent me) void mousePressed(MouseEvent me) void mouseReleased(MouseEvent me)

2/17/2016 DEPT OF CSE 53 The MouseMotionListener Interface void mouseDragged(MouseEvent me) void mouseMoved(MouseEvent me)

2/17/2016 DEPT OF CSE 54 The TextListener Interface Defines the textChanged( ) method that is invoked when a change occurs in a text area or text field void textChanged(TextEvent te)

2/17/2016 DEPT OF CSE 55 The WindowFocusListener Interface defines two methods: windowGainedFocus( ) and windowLostFocus( ) These are called when a window gains or losses input focus void windowGainedFocus(WindowEvent we) void windowLostFocus(WindowEvent we)

2/17/2016 DEPT OF CSE 56 The WindowListener Interface 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)

2/17/2016 DEPT OF CSE 57 Handling Mouse Events ExMouse.java // Demonstrate the mouse event handlers. import java.awt.*; import java.awt.event.*; import java.applet.*; /* */

2/17/2016 DEPT OF CSE 58 public class MouseEvents extends Applet implements MouseListener, MouseMotionListener { String msg = ""; int mouseX = 0, mouseY = 0; // coordinates of mouse public void init() { addMouseListener(this); //Registration addMouseMotionListener(this);//Registration } 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(); }

2/17/2016 DEPT OF CSE 59 public void mouseExited(MouseEvent me) { // save coordinates mouseX = 0; mouseY = 10; msg = "Mouse exited."; repaint(); } public void mousePressed(MouseEvent me) { // save coordinates mouseX = me.getX(); mouseY = me.getY(); msg = "Down"; repaint(); } public void mouseReleased(MouseEvent me) { // save coordinates mouseX = me.getX(); mouseY = me.getY(); msg = "Up"; repaint(); }

2/17/2016 DEPT OF CSE 60 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); }

2/17/2016 DEPT OF CSE 61 Handling Keyboard Eventssimplekey.java // 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); requestFocus(); // request input focus } public void keyPressed(KeyEvent ke) { showStatus("Key Down"); }

2/17/2016 DEPT OF CSE 62 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); }

2/17/2016 DEPT OF CSE 63 // Demonstrate some virtual key codes. Keyevents.java 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); requestFocus(); // request input focus }

2/17/2016 DEPT OF CSE 64 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;

2/17/2016 DEPT OF CSE 65 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(); }

2/17/2016 DEPT OF CSE 66 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); }

2/17/2016 DEPT OF CSE 67 Adapter Classes These are classes provided by java to simplify the event handling. These give default implementations to the methods in each listener interfaces such that whenever programmer needs to override only a few listener interface methods, giving blank implementations to the rest member methods can be avoided.

2/17/2016 DEPT OF CSE 68 List of such adapters are

2/17/2016 DEPT OF CSE 69 Using Adapter Classes listing 4 // Demonstrate an adaptor. import java.awt.*; import java.awt.event.*; import java.applet.*; /*

2/17/2016 DEPT OF CSE 70 Using Adapter Classes */ public class AdapterDemo extends Applet { public void init() { addMouseListener(new MyMouseAdapter(this)); addMouseMotionListener(new MyMouseMotionAdapter(this)); } class MyMouseAdapter extends MouseAdapter { AdapterDemo adapterDemo;

2/17/2016 DEPT OF CSE 71 Using Adapter Classes public MyMouseAdapter(AdapterDemo adapterDemo) { this.adapterDemo = adapterDemo; } // Handle mouse clicked. public void mouseClicked(MouseEvent me) { adapterDemo.showStatus("Mouse clicked"); } class MyMouseMotionAdapter extends MouseMotionAdapter {

2/17/2016 DEPT OF CSE 72 Using Adapter Classes AdapterDemo adapterDemo; public MyMouseMotionAdapter(AdapterDemo adapterDemo) { this.adapterDemo = adapterDemo; } // Handle mouse dragged. public void mouseDragged(MouseEvent me) { adapterDemo.showStatus("Mouse dragged"); }

2/17/2016 DEPT OF CSE 73 Using InnerClasses for Event Handling An inner class is a class defined inside another class or within an expression. This section shows us the benefit of above class in event handling Example usage as follows

2/17/2016 DEPT OF CSE 74 Inner class usage Ex: wihout inner class listing 5 // This applet does NOT use an inner class. import java.applet.*; import java.awt.event.*; /* */ public class MousePressedDemo extends Applet { public void init() {

2/17/2016 DEPT OF CSE 75 Inner class usage Ex: wihout inner class addMouseListener(new MyMouseAdapter(this)); } class MyMouseAdapter extends MouseAdapter { MousePressedDemo mousePressedDemo; public MyMouseAdapter(MousePressedDemo mousePressedDemo) { this.mousePressedDemo = mousePressedDemo; } public void mousePressed(MouseEvent me) { mousePressedDemo.showStatus("Mouse Pressed."); }

2/17/2016 DEPT OF CSE 76 Inner Class Usage Ex:With Inner class listing 6 // Inner class demo import java.applet.*; import java.awt.event.*; public class InnerClassDemo extends Applet { public void init() { addMouseListener(new MyMouseAdapter()); } class MyMouseAdapter extends MouseAdapter { public void mousePressed(MouseEvent me) { showStatus("Mouse Pressed"); }

2/17/2016 DEPT OF CSE 77 Anonymous Inner classes that extends MouseAdapter Inner classes without name listing 7 // Anonymous inner class demo. import java.applet.*; import java.awt.event.*; public class AnonymousInnerClassDemo extends Applet { public void init() { addMouseListener(new MouseAdapter() { public void mousePressed(MouseEvent me) { showStatus("Mouse Pressed"); } }); }