Event Handling Any program that uses GUI graphical

  • Slides: 84
Download presentation
Event Handling Any program that uses GUI (graphical user interface) such as Java application

Event Handling Any program that uses GUI (graphical user interface) such as Java application written for windows, is event driven. Event describes the change of state of any object. Events are generated as result of user interaction with the graphical user interface components. Department of Computer Engineering AJP Unit-III Mrs. Chavan P. P. 1

 • Changing the state of an object is known as an event. •

• Changing the state of an object is known as an event. • For example: clicking on a button, Entering a character in Textbox, moving the mouse, selecting an item from list, scrolling the page, etc. • The java. awt. event package provides many event classes and Listener interfaces for event handling. Department of Computer Engineering AJP Unit-III Mrs. Chavan P. P. 2

Delegation Event Model • The modern approach to handling events is based on the

Delegation Event Model • The modern approach to handling events is based on the delegation event model. • The delegation event model provides a standard mechanism for a source to generate an event and send it to a set of listeners. • The listener simply waits until it receives an event. • Once received, the listener processes the event and then return. Department of Computer Engineering AJP Unit-III Mrs. Chavan P. P. 3

 • In the delegation event model, listener must register with a source in

• In the delegation event model, listener must register with a source in order to receive an event notification. • Notification are sent only to listeners that want to receive them. • There are mainly three parts in delegation event model. – Events. – Event sources. – Event Listeners. Department of Computer Engineering AJP Unit-III Mrs. Chavan P. P. 4

Components of Event Handling Event handling has three main components: events, event sources, event

Components of Event Handling Event handling has three main components: events, event sources, event listeners. • Events An event is a change of state of an object. – Events may also occur that are not directly caused by interactions with a user interface. For example, an event may be generated when a timer expires, a counter exceeds a value, software or hardware failure occurs, or an operation is completed. Department of Computer Engineering AJP Unit-III Mrs. Chavan P. P. 5

Event Sources • A source is an object that generates an event. This occurs

Event Sources • A source is an object that generates an event. This occurs when the internal state of that object changes in some way. • 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. Department of Computer Engineering AJP Unit-III Mrs. Chavan P. P. 6

Event Listeners • A listener is an object that is notified when an event

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. • The method that receive and process events are defined in a set of interfaces found in java. awt. event. Department of Computer Engineering AJP Unit-III Mrs. Chavan P. P. 7

 • Here is the general form to register listeners: – public void add.

• Here is the general form to register listeners: – public void add. Type. Listener(Type. Listener el) – For example: b. add. Action. Listener(this); • Here, type is the name of the event, and el is a reference to the event listener. • For example, the method that registers a keyboard event listener is called add. Key. Listener(). • The method that registers a mouse motion listener is called add. Mouse. Motion. Listener(). • When an event occurs, all registered listeners are notified and receive a copy of the event object. Department of Computer Engineering AJP Unit-III Mrs. Chavan P. P. 8

 • The general form of unregister listener method is this: – Public void

• The general form of unregister listener method is this: – Public void remove. Type. Listener(Type. Listener el) • Here, type is an object that is notified when an event listener. For example, to remove a keyboard listener, you would call remove. Key. Listener() Department of Computer Engineering AJP Unit-III Mrs. Chavan P. P. 9

Department of Computer Engineering AJP Unit-III Mrs. Chavan P. P. 10

Department of Computer Engineering AJP Unit-III Mrs. Chavan P. P. 10

Important Event Classes and Interface Department of Computer Engineering AJP Unit-III Mrs. Chavan P.

Important Event Classes and Interface Department of Computer Engineering AJP Unit-III Mrs. Chavan P. P. 11

Event Classes Description Listener Interface Action. Event generated when button is pressed, menu-item is

Event Classes Description Listener Interface Action. Event generated when button is pressed, menu-item is selected, list -item is double clicked Action. Listener Mouse. Event generated when mouse is dragged, moved, clicked, pressed or released and also when it enters or exit a component Mouse. Listener Key. Event generated when input is received from keyboard Key. Listener Item. Event generated when check-box or list item is clicked Item. Listener Text. Event generated when value of textarea or textfield is changed Text. Listener Mouse. Wheel. Event Window. Event Component. Event Container. Event Adjustment. Event generated when mouse wheel is moved generated when window is activated, deactivated, deiconified, opened or closed generated when component is hidden, moved, resized or set visible generated when component is added or removed from container generated when scroll bar is manipulated Focus. Event generated when component gains or loses keyboard focus Department of Computer AJP Unit-III Mrs. Chavan P. P. Engineering Mouse. Wheel. Listener Window. Listener Component. Event. Listener Container. Listener Adjustment. Listener Focus. Listener 12

 • Steps to handle events: – Implement appropriate interface in the class. –

• Steps to handle events: – Implement appropriate interface in the class. – Register the component with the listener. Department of Computer Engineering AJP Unit-III Mrs. Chavan P. P. 13

Registration Methods For registering the component with the Listener, many classes provide the registration

Registration Methods For registering the component with the Listener, many classes provide the registration methods. For example: Button public void add. Action. Listener(Action. Listener a){} Menu. Item public void add. Action. Listener(Action. Listener a){} Text. Field public void add. Action. Listener(Action. Listener a){} public void add. Text. Listener(Text. Listener a){} Text. Area public void add. Text. Listener(Text. Listener a){} Checkbox public void add. Item. Listener(Item. Listener a){} Choice List public void add. Item. Listener(Item. Listener a){} public void add. Action. Listener(Action. Listener a){} Department of Computer AJP Unit-III Mrs. Chavan P. P. public void add. Item. Listener(Item. Listener a){} Engineering 14

Action. Event • An Action. Event is generated when a button is pressed, a

Action. Event • An Action. Event is generated when a button is pressed, a list item is double-clicked, or a menu item is selected. • The Action. Event class defines four integer constants that can be used to identify any modifiers associated with an action event: • public static final int ALT_MASK – The alt modifier. An indicator that the alt key was held down during the event. (8) • public static final int SHIFT_MASK – The shift modifier. An indicator that the shift key was held down during the event. (1) • public static final int CTRL_MASK – The control modifier. An indicator that the control key was held down during the event. (2) • public static final int META_MASK – The meta modifier. An indicator that the meta key was held down Department of Computer AJP Unit-III Mrs. Chavan P. P. 15 during the event. (The Meta key is a modifier key on certain keyboards) (4) Engineering

public Action. Event(Object source, int id, String command, int modifiers) – Constructs an Action.

public Action. Event(Object source, int id, String command, int modifiers) – Constructs an Action. Event object with modifier keys. – Parameters: source - the object that originated the event – id - an integer that identifies the event – command - a string that may specify a command (possibly one of several) associated with the event – modifiers - the modifier keys held down during this action Department of Computer Engineering AJP Unit-III Mrs. Chavan P. P. 16

public Action. Event(Object source, int id, String command, long when, int modifiers) Constructs an

public Action. Event(Object source, int id, String command, long when, int modifiers) Constructs an Action. Event object with the specified modifier keys and timestamp. Parameters: source - the object that originated the event id - an integer that identifies the event command - a string that may specify a command (possibly one of several) associated with the event when - the time the event occurred modifiers - the modifier keys held down during this action Department of Computer Engineering AJP Unit-III Mrs. Chavan P. P. 17

Methods • public String get. Action. Command() – Returns the command string associated with

Methods • public String get. Action. Command() – Returns the command string associated with this action. • public long get. When() – Returns the timestamp of when this event occurred. • int get. Modifiers() – Returns the modifier keys held down during this action event. • String param. String() – Returns a parameter string identifying this action Department of Computer event. AJP Unit-III Mrs. Chavan P. P. Engineering 18

Action. Listener Interface • This interface defines the action. Performed() method that is invoked

Action. Listener Interface • This interface defines the action. Performed() method that is invoked when an action event occurs. • Its general form is shown here: – void action. Performed(Action. Event ae) Department of Computer Engineering AJP Unit-III Mrs. Chavan P. P. 19

import java. applet. *; import java. awt. event. *; /* <applet code="Action. Event. Example"

import java. applet. *; import java. awt. event. *; /* <applet code="Action. Event. Example" width=200 height=200> </applet> */ public class Action. Event. Example extends Applet implements Action. Listener { String action. Message=""; public void init() { Button 1 = new Button("Ok"); Button 2 = new Button("Cancel"); add(Button 1); add(Button 2); Button 1. add. Action. Listener(this); //Listener Registered Button 2. add. Action. Listener(this); //Listener Registered Department of Computer AJP Unit-III Mrs. Chavan P. P. } Engineering 20

public void paint(Graphics g) { g. draw. String(action. Message, 10, 50); } public void

public void paint(Graphics g) { g. draw. String(action. Message, 10, 50); } public void action. Performed(Action. Event ae) { String action = ae. get. Action. Command(); if(action. equals("Ok")) action. Message = "Ok Button Pressed"; else if(action. equals("Cancel")) action. Message = "Cancel Button Pressed"; repaint(); } } Department of Computer Engineering AJP Unit-III Mrs. Chavan P. P. 21

Department of Computer Engineering AJP Unit-III Mrs. Chavan P. P. 22

Department of Computer Engineering AJP Unit-III Mrs. Chavan P. P. 22

Component. Event class • A low-level event which indicates that a component moved, changed

Component. Event class • A low-level event which indicates that a component moved, changed size, or changed visibility. • This class has following constants. • public static final int COMPONENT_MOVED – This event indicates that the component's position changed. • public static final int COMPONENT_RESIZED – This event indicates that the component's size changed. • public static final int COMPONENT_SHOWN – This event indicates that the component was made visible. • public static final int COMPONENT_HIDDEN – This event indicates that the component was become invisible. Department of Computer Engineering AJP Unit-III Mrs. Chavan P. P. 23

 • public Component. Event(Component source, int id) – Constructs a Component. Event object.

• public Component. Event(Component source, int id) – Constructs a Component. Event object. Parameters: – source - the Component that originated the event – id - an integer indicating the type of event • Component get. Component() – Returns the creator of the event. – the Component object that originated the event, or null if the object is not a Component. Department of Computer Engineering AJP Unit-III Mrs. Chavan P. P. 24

Component. LIstener interface • The listener interface for receiving component events. • void component.

Component. LIstener interface • The listener interface for receiving component events. • void component. Resized(Component. Event e) – Invoked when the component's size changes. • void component. Moved(Component. Event e) – Invoked when the component's position changes • void component. Shown(Component. Event e) – Invoked when the component has been made visible. • void component. Hidden(Component. Event e) – Invoked when the component has been made invisible. Department of Computer Engineering AJP Unit-III Mrs. Chavan P. P. 25

import java. awt. *; import java. awt. event. *; import javax. swing. *; public

import java. awt. *; import java. awt. event. *; import javax. swing. *; public class Component. Event. Example 1 { public static void main(String[] args) { JFrame frame = new JFrame("Component. Event. Example"); frame. set. Default. Close. Operation(JFrame. EXIT_ON_CLOSE); Text. Area txt. Area = new Text. Area(); Checkbox checkbox 1 = new Checkbox("Checkbox 1"); Checkbox checkbox 2 = new Checkbox("Checkbox 2"); frame. add(txt. Area, Border. Layout. CENTER); frame. add(checkbox 1, Border. Layout. NORTH); frame. add(checkbox 2, Border. Layout. SOUTH); frame. set. Visible(true); Component. Listener component. Listener = new My. Comp. List(); frame. add. Component. Listener(component. Listener); } Department of Computer } AJP Unit-III Mrs. Chavan P. P. Engineering 26

class My. Comp. List implements Component. Listener { public void component. Shown(Component. Event evt)

class My. Comp. List implements Component. Listener { public void component. Shown(Component. Event evt) { System. out. println("component. Shown"); } public void component. Hidden(Component. Event evt) { System. out. println("component. Hidden"); } public void component. Moved(Component. Event evt) { System. out. println("component. Moved"); } public void component. Resized(Component. Event evt) { System. out. println("component. Resized"); } } Department of Computer AJP Unit-III Mrs. Chavan P. P. Engineering 27

Department of Computer Engineering AJP Unit-III Mrs. Chavan P. P. 28

Department of Computer Engineering AJP Unit-III Mrs. Chavan P. P. 28

Container. Event class • A low-level event which indicates that a container's contents changed

Container. Event class • A low-level event which indicates that a container's contents changed because a component was added or removed • This class has following constants. • public static final int COMPONENT_ADDED – This event indicates that a component was added to the container. • public static final int COMPONENT_REMOVED – This event indicates that a component was removed from the container. Department of Computer Engineering AJP Unit-III Mrs. Chavan P. P. 29

 • public Container. Event(Component source, int id, Component child) – Constructs a Container.

• public Container. Event(Component source, int id, Component child) – Constructs a Container. Event object. Parameters: – source - the Component object (container) that originated the event – id - an integer indicating the type of event – child - the component that was added or removed Department of Computer Engineering AJP Unit-III Mrs. Chavan P. P. 30

 • public Container get. Container() – Returns the originator of the event. –

• public Container get. Container() – Returns the originator of the event. – Returns the Container object that originated the event, or null if the object is not a Container. • public Component get. Child() – Returns the component that was affected by the event. – Returns the Component object that was added or removed. Department of Computer Engineering AJP Unit-III Mrs. Chavan P. P. 31

Container. Listener interface – The listener interface for receiving container events. • void component.

Container. Listener interface – The listener interface for receiving container events. • void component. Added(Container. Event e) – Invoked when a component has been added to the container. • void component. Removed (Container. Event e) – Invoked when a component has been removed from the container. Department of Computer Engineering AJP Unit-III Mrs. Chavan P. P. 32

import java. awt. *; import java. awt. event. *; import javax. swing. *; public

import java. awt. *; import java. awt. event. *; import javax. swing. *; public class Container. Event. Example { public static void main(String args[]) { JFrame frame = new JFrame(); frame. set. Default. Close. Operation(JFrame. EXIT_ON_CLOSE); Container content. Pane = frame. get. Content. Pane(); Container. Listener cont = new Container. Listener() { Action. Listener listener = new Action. Listener() { public void action. Performed(Action. Event e) { System. out. println("Selected: " + e. get. Action. Command()); }; Department of Computer AJP Unit-III Mrs. Chavan P. P. Engineering 33

 public void component. Added(Container. Event e) { Component c = e. get. Child();

public void component. Added(Container. Event e) { Component c = e. get. Child(); if (c instanceof JButton) { JButton b = (JButton) c; b. add. Action. Listener(listener); } } public void component. Removed(Container. Event e) { Component c = e. get. Child(); if (c instanceof JButton) { JButton b = (JButton) c; b. remove. Action. Listener(listener); } } }; Department of Computer Engineering AJP Unit-III Mrs. Chavan P. P. 34

 content. Pane. add. Container. Listener(cont); content. Pane. set. Layout(new Grid. Layout(3, 2)); content.

content. Pane. add. Container. Listener(cont); content. Pane. set. Layout(new Grid. Layout(3, 2)); content. Pane. add(new JButton("First")); content. Pane. add(new JButton("Second")); content. Pane. add(new JButton("Third")); content. Pane. add(new JButton("Fourth")); content. Pane. add(new JButton("Fifth")); frame. set. Size(300, 200); frame. show(); } } Department of Computer Engineering AJP Unit-III Mrs. Chavan P. P. 35

Department of Computer Engineering AJP Unit-III Mrs. Chavan P. P. 36

Department of Computer Engineering AJP Unit-III Mrs. Chavan P. P. 36

Focus. Event class • A low-level event which indicates that a Component has gained

Focus. Event class • A low-level event which indicates that a Component has gained or lost the input focus. • This class has following constants. • public static final int FOCUS_GAINED – This event indicates that the Component is now the focus owner. • public static final int FOCUS_LOST – This event indicates that the Component is no longer the focus owner. Department of Computer Engineering AJP Unit-III Mrs. Chavan P. P. 37

Constructors • public Focus. Event(Component source, int id, boolean temporary, Component opposite) – source

Constructors • public Focus. Event(Component source, int id, boolean temporary, Component opposite) – source - the Component that originated the event – id - FOCUS_GAINED or FOCUS_LOST – temporary - true if the focus change is temporary; false otherwise – opposite - the other Component involved in the focus change, or null Department of Computer Engineering AJP Unit-III Mrs. Chavan P. P. 38

 • public Focus. Event(Component source, int id, boolean temporary) – id - an

• public Focus. Event(Component source, int id, boolean temporary) – id - an integer indicating the type of event – temporary - true if the focus change is temporary; false otherwise. • public Focus. Event(Component source, int id) – source - the Component that originated the event – id - an integer indicating the type of event Department of Computer Engineering AJP Unit-III Mrs. Chavan P. P. 39

Methods • public boolean is. Temporary() – Identifies the focus change event as temporary

Methods • public boolean is. Temporary() – Identifies the focus change event as temporary or permanent. – Returns: true if the focus change is temporary; false otherwise • public Component get. Opposite. Component() – Returns the other Component involved in this focus change. Department of Computer Engineering AJP Unit-III Mrs. Chavan P. P. 40

Focus. Listener interface • void focus. Gained(Focus. Event e) – Invoked when a component

Focus. Listener interface • void focus. Gained(Focus. Event e) – Invoked when a component gains the keyboard focus. • void focus. Lost(Focus. Event e) – Invoked when a component loses the keyboard focus. Department of Computer Engineering AJP Unit-III Mrs. Chavan P. P. 41

import java. awt. *; import java. awt. event. *; import javax. swing. *; public

import java. awt. *; import java. awt. event. *; import javax. swing. *; public class Focus. Listener. Example extends JFrame implements Focus. Listener { Button b 1, b 2; public Focus. Listener. Example() { b 1=new Button ("First"); b 2=new Button ("Second"); add(b 1, Border. Layout. SOUTH); add(b 2, Border. Layout. NORTH); b 1. add. Focus. Listener(this); b 2. add. Focus. Listener(this); set. Size(200, 200); set. Default. Close. Operation(JFrame. EXIT_ON_CLOSE); set. Visible(true); Department of Computer AJP Unit-III Mrs. Chavan P. P. } Engineering 42

 public void focus. Gained(Focus. Event fe) //method of focuslistener { if(fe. get. Source()==b

public void focus. Gained(Focus. Event fe) //method of focuslistener { if(fe. get. Source()==b 1) System. out. println(b 1. get. Label()+"gained"); if(fe. get. Source()==b 2) System. out. println(b 2. get. Label()+"gained"); if(fe. is. Temporary()) System. out. println("Temporary Focus"); } public void focus. Lost(Focus. Event fe) //in focusevent "get. ID()"is a method { if(fe. get. Source()==b 1) System. out. println(b 1. get. Label()+"lost"); if(fe. get. Source()==b 2) System. out. println(b 2. get. Label()+"lost"); } public static void main(String a[]) { new Focus. Listener. Example(); } Department of Computer AJP Unit-III Mrs. Chavan P. P. 43 } Engineering

Department of Computer Engineering AJP Unit-III Mrs. Chavan P. P. 44

Department of Computer Engineering AJP Unit-III Mrs. Chavan P. P. 44

Item. Event class • A semantic event which indicates that an item was selected

Item. Event class • A semantic event which indicates that an item was selected or deselected. • This high-level event is generated by an Item. Selectable object (such as a List) when an item is selected or deselected by the user. • This class has following constants. • public static final int SELECTED – This state-change value indicates that an item was selected. • public static final int DESELECTED – This state-change-value indicates that a selected item was deselected Department of Computer Engineering AJP Unit-III Mrs. Chavan P. P. 45

 • public Item. Event (Item. Selectable source, int id, Object item, int state.

• public Item. Event (Item. Selectable source, int id, Object item, int state. Change) – Constructs an Item. Event object. – Parameters: – source - the Item. Selectable object that originated the event – id - an integer that identifies the event type – item - an object -- the item affected by the event – state. Change - an integer that indicates whether the item was selected or deselected Department of Computer Engineering AJP Unit-III Mrs. Chavan P. P. 46

Methods of Item. Event Class • public Item. Selectable get. Item. Selectable() – Returns

Methods of Item. Event Class • public Item. Selectable get. Item. Selectable() – Returns the creator of the event. – Returns: the Item. Selectable object that originated the event. • public Object get. Item() – Returns the item affected by the event. – Returns: the item (object) that was affected by the event. • public int get. State. Change() – Returns the type of state change (selected or deselected). – Returns: an integer that indicates whether the item was selected or deselected Department of Computer Engineering AJP Unit-III Mrs. Chavan P. P. 47

Item. Listener interface • The listener interface for receiving item events. • void item.

Item. Listener interface • The listener interface for receiving item events. • void item. State. Changed(Item. Event e) • Invoked when an item has been selected or deselected by the user. • The code written for this method performs the operations that need to occur when an item is selected (or deselected). Department of Computer Engineering AJP Unit-III Mrs. Chavan P. P. 48

import java. applet. *; import java. awt. event. *; /* <applet code="Item. Listener. Example"

import java. applet. *; import java. awt. event. *; /* <applet code="Item. Listener. Example" width=200 height=200> </applet> */ public class Item. Listener. Example extends Applet implements Item. Listener { Checkbox java = null; Checkbox vb = null; Checkbox c = null; Department of Computer Engineering AJP Unit-III Mrs. Chavan P. P. 49

 public void init() { java = new Checkbox("Java"); vb = new Checkbox("Visual Basic");

public void init() { java = new Checkbox("Java"); vb = new Checkbox("Visual Basic"); c = new Checkbox("C"); add(java); add(vb); add(c); java. add. Item. Listener(this); vb. add. Item. Listener(this); c. add. Item. Listener(this); } public void paint(Graphics g) { g. draw. String("Java: " + java. get. State(), 10, 80); g. draw. String("VB: " + vb. get. State(), 100); g. draw. String("C: " + c. get. State(), 10, 120); } public void item. State. Changed(Item. Event ie) { repaint(); } Department of Computer AJP Unit-III Mrs. Chavan P. P. Engineering } 50

Department of Computer Engineering AJP Unit-III Mrs. Chavan P. P. 51

Department of Computer Engineering AJP Unit-III Mrs. Chavan P. P. 51

Key. Event class • An event which indicates that a keystroke occurred in a

Key. Event class • An event which indicates that a keystroke occurred in a component. • This class has following constant. • public static final int KEY_PRESSED • The "key pressed" event. This event is generated when a key is pushed down. • public static final int KEY_RELEASED • The "key released" event. This event is generated when a key is let up. • public static final int KEY_TYPED • The "key typed" event. This event is generated when a character is entered. In the simplest case, it is produced by a single key press. Often, however, characters are produced by series of key presses, and the mapping from key pressed events to key typed events may be many-to-one or many-to-many. Department of Computer Engineering AJP Unit-III Mrs. Chavan P. P. 52

 • There are many other integer constants that are defined by Key. Event.

• There are many other integer constants that are defined by Key. Event. For example – VK_0 to VK_9 – VK_A to VK_Z define the ASCII equivalents of the numbers and letters. • Here are some others: – 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 VK constants specify virtual key codes and are independent of any modifiers, such as control, shift, or alt. Department of Computer Engineering AJP Unit-III Mrs. Chavan P. P. 53

Methods of Key. Event class • public int get. Key. Code() – Returns the

Methods of Key. Event class • public int get. Key. Code() – Returns the integer key. Code associated with the key in this event. – Returns: the integer code for an actual key on the keyboard. • public char get. Key. Char() – Returns the character associated with the key in this event. – For example, the KEY_TYPED event for shift + "a" returns the value for "A". • boolean is. Action. Key() – Returns true if the key firing the event is an action key. Examples of action keys include Page Up, Caps Lock, the arrow and function keys. Department of Computer Engineering AJP Unit-III Mrs. Chavan P. P. 54

Key. Listener Interface • Key events indicate when the user is typing at the

Key. Listener Interface • Key events indicate when the user is typing at the keyboard. • Key events are fired by the component with the keyboard focus when the user presses or releases keyboard keys. • Notifications are sent about two basic kinds of key events: – The typing of a Unicode character – The pressing or releasing of a key on the keyboard Department of Computer Engineering AJP Unit-III Mrs. Chavan P. P. 55

 • The first kind of event is called a key-typed event. • To

• The first kind of event is called a key-typed event. • To know when the user types a Unicode character ? whether by pressing one key such as 'a' or by pressing several keys in sequence ? • The second kind is either a key-pressed or key-released event. • To know when the user presses the F 1 key, or whether the user pressed the '3' key on the number pad, you handle key-pressed events. Department of Computer Engineering AJP Unit-III Mrs. Chavan P. P. 56

Methods of Key. Listener Interface Method Purpose key. Typed(Key. Event) Called just after the

Methods of Key. Listener Interface Method Purpose key. Typed(Key. Event) Called just after the user types a Unicode character into the listenedto component. key. Pressed(Key. Event) Called just after the user presses a key while the listened-to component has the focus. key. Released(Key. Event) Called just after the user releases a key while the listened-to component has the focus. Department of Computer Engineering AJP Unit-III Mrs. Chavan P. P. 57

import java. awt. *; import java. awt. event. *; import javax. swing. JApplet; public

import java. awt. *; import java. awt. event. *; import javax. swing. JApplet; public class Event. Demo 6 extends JApplet implements Key. Listener { String event; // description of keyboard event public void init() // set up UI { set. Layout(new Flow. Layout()); event = ""; add. Key. Listener(this); // listen for keyboard events set. Focusable(true); // force applet to receive Key. Event } public void paint(Graphics g) // draw message to applet { super. paint(g); g. draw. Rect(0, 0, get. Width(), get. Height()); // show bounds of applet g. draw. String(event, 10, 50); } Department of Computer Engineering AJP Unit-III Mrs. Chavan P. P. 58

 public void key. Pressed(Key. Event e) // handle key presses { event =

public void key. Pressed(Key. Event e) // handle key presses { event = e. get. Key. Char() + " pressed"; repaint(); } public void key. Released(Key. Event e) // handle key releases { event = e. get. Key. Char() + " released"; repaint(); } public void key. Typed(Key. Event e) // handle typing on applet { event = e. get. Key. Char() + " typed"; repaint(); } } Department of Computer Engineering AJP Unit-III Mrs. Chavan P. P. 59

Text. Event class • A semantic event which indicates that an object's text changed.

Text. Event class • A semantic event which indicates that an object's text changed. • This high-level event is generated by an object (such as a Text. Component) when its text changes. • public Text. Event(Object source, int id) – Constructs a Text. Event object. – Parameters: – source - the (Text. Component) object that originated the event – id - an integer that identifies the event type Department of Computer Engineering AJP Unit-III Mrs. Chavan P. P. 60

Text. Listener interface • The listener interface for receiving text events. • void text.

Text. Listener interface • The listener interface for receiving text events. • void text. Value. Changed(Text. Event e) • Invoked when the value of the text has changed. • The code written for this method performs the operations that need to occur when text changes. Department of Computer Engineering AJP Unit-III Mrs. Chavan P. P. 61

Window. Event class • A low-level event indicates that a window has changed its

Window. Event class • A low-level event indicates that a window has changed its status. • This event is generated by a Window object when it is opened, closed, activated, deactivated, iconified, or deiconified, or when focus is transferred into or out of the Window. Department of Computer Engineering AJP Unit-III Mrs. Chavan P. P. 62

int constants • • • WINDOW_ACTIVATED WINDOW_CLOSING WINDOW_DEACTIVATED WINDOW_DEICONIFIED WINDOW_GAINED_FOCUS WINDOW_ICONIFIED WINDOW_LOST_FOCUS WINDOW_OPENED WINDOW_STATE_CHANGED

int constants • • • WINDOW_ACTIVATED WINDOW_CLOSING WINDOW_DEACTIVATED WINDOW_DEICONIFIED WINDOW_GAINED_FOCUS WINDOW_ICONIFIED WINDOW_LOST_FOCUS WINDOW_OPENED WINDOW_STATE_CHANGED Department of Computer Engineering AJP Unit-III Mrs. Chavan P. P. 63

Constructors • public Window. Event(Window source, int id) – Constructs a Window. Event object.

Constructors • public Window. Event(Window source, int id) – Constructs a Window. Event object. – Parameters: source - the Window object that originated the event – id - an integer indicating the type of event • - public Window. Event(Window source, int id, Window opposite, int old. State, int new. State) – Note that passing in an invalid id results in unspecified behavior. This method throws an Illegal. Argument. Exception if source is null. – source - the Window object that originated the event – id - an integer indicating the type of event. – opposite - the other window involved in the focus or activation change, or null – old. State - previous state of the window for window state change event – new. State - new state of the window for window state change event Department of Computer Engineering AJP Unit-III Mrs. Chavan P. P. 64

 • public Window get. Window() – Returns the originator of the event. –

• public Window get. Window() – Returns the originator of the event. – Returns: the Window object that originated the event Department of Computer Engineering AJP Unit-III Mrs. Chavan P. P. 65

Window. Listener interface • void window. Opened(Window. Event e) – Invoked the first time

Window. Listener interface • void window. Opened(Window. Event e) – Invoked the first time a window is made visible. • void window. Closing(Window. Event e) – Invoked when the user attempts to close the window from the window's system menu. • void window. Closed(Window. Event e) – Invoked when a window has been closed as the result of calling dispose on the window • void window. Iconified(Window. Event e) – Invoked when a window is changed from a normal to a minimized state. For many platforms, a minimized window is displayed as the icon specified in the window's icon. Image property. • void window. Deiconified(Window. Event e) – Invoked when a window is changed from a minimized to a normal state. • void window. Activated(Window. Event e) – Invoked when the Window is set to be the active Window. • void window. Deactivated(Window. Event e) – Invoked when a Window is no longer the active Window. Department of Computer Engineering AJP Unit-III Mrs. Chavan P. P. 66

Window. Focus. Listener interface • The listener interface for receiving Window. Events, including WINDOW_GAINED_FOCUS

Window. Focus. Listener interface • The listener interface for receiving Window. Events, including WINDOW_GAINED_FOCUS and WINDOW_LOST_FOCUS events. • void window. Gained. Focus(Window. Event e) – Invoked when the Window is set to be the focused Window, which means that the Window, or one of its subcomponents, will receive keyboard events. • void window. Lost. Focus(Window. Event e) – Invoked when the Window is no longer the focused Window, which means that keyboard events will no longer be delivered to the Window or any of its subcomponents. Department of Computer Engineering AJP Unit-III Mrs. Chavan P. P. 67

Mouse. Event class This event indicates a mouse action occurred in a component. This

Mouse. Event class This event indicates a mouse action occurred in a component. This low-level event is generated by a component object for Mouse Events and Mouse motion events. Department of Computer Engineering AJP Unit-III Mrs. Chavan P. P. 68

Constants for java. awt. event. Mouse. Event class: static int BUTTON 1 --Indicates mouse

Constants for java. awt. event. Mouse. Event class: static int BUTTON 1 --Indicates mouse button #1; used by get. Button() static int BUTTON 2 --Indicates mouse button #2; used by get. Button() static int BUTTON 3 --Indicates mouse button #3; used by get. Button() static int MOUSE_CLICKED --The "mouse clicked" event static int MOUSE_DRAGGED --The "mouse dragged" event static int MOUSE_ENTERED --The "mouse entered" event static int MOUSE_EXITED --The "mouse exited" event static int MOUSE_MOVED --The "mouse moved" event static int MOUSE_PRESSED -- The "mouse pressed" event Department of Computer Engineering AJP Unit-III Mrs. Chavan P. P. 69

Constructor • Mouse. Event(Component source, int id, long when, int modifiers, int x, int

Constructor • Mouse. Event(Component source, int id, long when, int modifiers, int x, int y, int click. Count, boolean popup. Trigger) – Constructs a Mouse. Event object with the specified source- source component, id- type of event, when- system time at mouse event occurred modifiers-to know what modifiers were pressed after event was occurred, x & y- coordinates of the mouse , click. Count- click count popup. Trigger- whether popup menu appeared Department of Computer Engineering AJP Unit-III Mrs. Chavan P. P. 70

Method Purpose int get. Click. Count() Returns the number of quick, consecutive clicks the

Method Purpose int get. Click. Count() Returns the number of quick, consecutive clicks the user has made (including this event). For example, returns 2 for a double click. int get. Button() Returns which mouse button, if any, has a changed state. One of the following constants is returned: NOBUTTON, BUTTON 1, BUTTON 2, or BUTTON 3. int get. X() int get. Y() Return the (x, y) position at which the event occurred, relative to the component that fired the event. Point get. Point() Returns the x, y position of the event rlative to the source component. Department of Computer Engineering AJP Unit-III Mrs. Chavan P. P. 71

Mouse. Listener Interface • Mouse events notify when the user uses the mouse (or

Mouse. Listener Interface • Mouse events notify when the user uses the mouse (or similar input device) to interact with a component. • Mouse events occur when the cursor enters or exits a component's onscreen area and when the user presses or releases one of the mouse buttons. Department of Computer Engineering AJP Unit-III Mrs. Chavan P. P. 72

Methods of Mouse. Listener Interface Method Purpose mouse. Clicked(Mouse. Event) Called just after the

Methods of Mouse. Listener Interface Method Purpose mouse. Clicked(Mouse. Event) Called just after the user clicks the listened-to component. mouse. Entered(Mouse. Event) Called just after the cursor enters the bounds of the listened-to component. mouse. Exited(Mouse. Event) Called just after the cursor exits the bounds of the listened-to component. mouse. Pressed(Mouse. Event) Called just after the user presses a mouse button while the cursor is over the listened-to component. mouse. Released(Mouse. Event) Called just after the user releases a mouse button after a mouse press over the listened-to component. Department of Computer Engineering AJP Unit-III Mrs. Chavan P. P. 73

Mouse. Motion. Listener Interface • Mouse-motion events notify when the user uses the mouse

Mouse. Motion. Listener Interface • Mouse-motion events notify when the user uses the mouse (or a similar input device) to move the onscreen cursor. • If an application requires the detection of both mouse events and mouse-motion events, use the Mouse. Input. Adapter class. • It implements the Mouse. Input. Listener a convenient interface that implements both the Mouse. Listener and Mouse. Motion. Listener interfaces. Department of Computer Engineering AJP Unit-III Mrs. Chavan P. P. 74

Methods of Mouse. Motion. Listener Interface Method Purpose mouse. Dragged(Mouse. Event) mouse. Moved(Mouse. Event)

Methods of Mouse. Motion. Listener Interface Method Purpose mouse. Dragged(Mouse. Event) mouse. Moved(Mouse. Event) Department of Computer Engineering Called in response to the user moving the mouse while holding a mouse button down. This event is fired by the component that fired the most recent mouse-pressed event, even if the cursor is no longer over that component. Called in response to the user moving the mouse with no mouse buttons pressed. This event is fired by the component that's currently under the cursor. AJP Unit-III Mrs. Chavan P. P. 75

import java. awt. *; import java. awt. event. *; import javax. swing. *; public

import java. awt. *; import java. awt. event. *; import javax. swing. *; public class Mouse. Event. Demo extends JApplet implements Mouse. Listener { private int x; // x coordinate of mouse event private int y; // y coordinate of mouse event private String event; // description of mouse event public void init() // set up GUI { set. Layout(new Flow. Layout()); add. Mouse. Listener(this); // listen for mouse events x = -1; // set x negative for no initial message } Department of Computer Engineering AJP Unit-III Mrs. Chavan P. P. 76

 public void paint(Graphics g) // draw message to screen { super. paint(g); g.

public void paint(Graphics g) // draw message to screen { super. paint(g); g. draw. Rect(0, 0, get. Width(), get. Height()); // show bounds of applet if(x != - 1) // display event during repainting only { g. draw. String("Mouse event " + event + " at (" + x + ", " + y + ")", 10, 50); } } public void mouse. Pressed(Mouse. Event e) // save coordinates of presses { x = e. get. X(); y = e. get. Y(); event = "press"; repaint(); } Department of Computer Engineering AJP Unit-III Mrs. Chavan P. P. 77

public void mouse. Clicked(Mouse. Event e) // save coordinates of clicks { x =

public void mouse. Clicked(Mouse. Event e) // save coordinates of clicks { x = e. get. X(); y = e. get. Y(); event = "click"; repaint(); } public void mouse. Released(Mouse. Event e) // save coordinates of releases { x = e. get. X(); y = e. get. Y(); event = "release"; repaint(); } Department of Computer Engineering AJP Unit-III Mrs. Chavan P. P. 78

 public void mouse. Entered(Mouse. Event e) // save coordinates when mouse enters applet

public void mouse. Entered(Mouse. Event e) // save coordinates when mouse enters applet { x = e. get. X(); y = e. get. Y(); event = "enter"; repaint(); } public void mouse. Exited(Mouse. Event e) // save coordinates when mouse leaves applet { x = e. get. X(); y = e. get. Y(); event = "exit"; repaint(); } } Department of Computer /*<applet code=Mouse. Event. Demo height=300 width=300></applet>*/ AJP Unit-III Mrs. Chavan P. P. Engineering 79

Summary of Event Classes & Listeners Department of Computer Engineering AJP Unit-III Mrs. Chavan

Summary of Event Classes & Listeners Department of Computer Engineering AJP Unit-III Mrs. Chavan P. P. 80

Department of Computer Engineering AJP Unit-III Mrs. Chavan P. P. 81

Department of Computer Engineering AJP Unit-III Mrs. Chavan P. P. 81

Department of Computer Engineering AJP Unit-III Mrs. Chavan P. P. 82

Department of Computer Engineering AJP Unit-III Mrs. Chavan P. P. 82

Department of Computer Engineering AJP Unit-III Mrs. Chavan P. P. 83

Department of Computer Engineering AJP Unit-III Mrs. Chavan P. P. 83

Department of Computer Engineering AJP Unit-III Mrs. Chavan P. P. 84

Department of Computer Engineering AJP Unit-III Mrs. Chavan P. P. 84