Event Handling Basics Event handling is fundamental to

  • Slides: 37
Download presentation
Event Handling

Event Handling

Basics • Event handling is fundamental to Java programming because it is integral to

Basics • Event handling is fundamental to Java programming because it is integral to the creation of applets and other types of GUI-based programs. • Events are supported by a number of packages, including java. util, java. awt, and java. awt. event.

Two Event Handling Mechanisms • The way in which events are handled changed significantly

Two Event Handling Mechanisms • The way in which events are handled changed significantly between the original version of Java (1. 0) and modern versions of Java, beginning with version 1. 1. • The 1. 0 method of event handling is still supported, but it is not recommended for new programs. • Also, many of the methods that support the old 1. 0 event model have been deprecated.

The 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. • Concept: a source generates an event and sends it to one or more listeners. • In this scheme, the listener simply waits until it receives an event. • Once an event is received, the listener processes the event and then returns. • 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. • A user interface element is able to “delegate” the processing of an event to a separate piece of code.

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

• In the delegation event model, listeners must register with a source in order to receive an event notification. • This provides an important benefit: notifications are sent only to listeners that want to receive them.

Events • In the delegation model, an event is an object that describes a

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 GUI. • 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. • 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, a software or hardware failure occurs, or an operation is completed.

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. • 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. Here is the general form: public void add. Type. Listener(Type. Listener el) Here, Type is the name of the event, and el is a reference to the event listener. Example: add. Key. Listener( ), add. Mouse. Motion. Listener( )

 • When an event occurs, all registered listeners are notified and receive a

• When an event occurs, all registered listeners are notified and receive a copy of the event object. • Some sources may allow only one listener to register. • The general form is: public void add. Type. Listener(Type. Listener el) throws java. util. Too. Many. Listeners. Exception • A source must also provide a method that allows a listener to unregister an interest in a specific type of event. The general form is: public void remove. Type. Listener(Type. Listener el)

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 methods that receive and process events are defined in a set of interfaces found in java. awt. event. • For ex, the Mouse. Motion. Listener interface defines two methods to receive notifications when the

Event Classes • The most widely used events are those defined by the AWT

Event Classes • The most widely used events are those defined by the AWT and those defined by Swing. • At the root of the Java event class hierarchy is Event. Object, which is in java. util. • It is the superclass for all events. • It has one constructor : Event. Object(Object src) Here, src is the object that generates this event. • It has following methods: • Object get. Source( ) : returns the source of the event • to. String( ): returns the string equivalent of the event.

 • The class AWTEvent, defined within the java. awt package, is a subclass

• The class AWTEvent, defined within the java. awt package, is a subclass of Event. Object. • It is the superclass (either directly or indirectly) of all AWT-based events used by the delegation event model. • Its get. ID( ) method can be used to determine the type of the event. int get. ID( )

The Action. Event Class • An Action. Event is generated when a button is

The Action. Event Class • 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: 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.

 • Action. Event has these three constructors: Action. Event(Object src, int type, String

• Action. Event has these three constructors: Action. Event(Object src, int type, String cmd) Action. Event(Object src, int type, String cmd, int modifiers) Action. Event(Object src, int type, String cmd, long when, int modifiers) • You can obtain the command name for the invoking Action. Event object by using the get. Action. Command( ) method, shown here: String get. Action. Command( ) • The get. Modifiers( ) method returns a value that indicates which modifier keys (ALT, CTRL, META, and/or SHIFT) were pressed when the event was generated. Its form is shown here: int get. Modifiers( ) • The method get. When( ) returns the time at which the event took place. This is called the event’s timestamp. long get. When( )

The Adjustment. Event Class • An Adjustment. Event is generated by a scroll bar.

The Adjustment. Event Class • An Adjustment. Event is generated by a scroll bar. • There are five types of adjustment events. Which are identified by following integer constants. BLOCK_DECREMENT: The user clicked inside the scroll bar to decrease its value. BLOCK_INCREMENT : The user clicked inside the scroll bar to increase its value. TRACK : The slider was dragged. UNIT_DECREMENT : The button at the end of the scroll bar was clicked to decrease its value. UNIT_INCREMENT : The button at the end of the scroll bar was clicked to increase its value.

 • Here is one Adjustment. Event constructor: Adjustment. Event(Adjustable src, int id, int

• Here is one Adjustment. Event constructor: Adjustment. Event(Adjustable src, int id, int type, int data) Here, src is a reference to the object that generated this event. id specifies the event. type of the adjustment is specified by type, and its associated data is data. • Methods: 1. Adjustable get. Adjustable( ) : returns the object that generated the event. 2. int get. Adjustment. Type( ): Returns the type of the adjustment event. 3. int get. Value( ): Returns the amount of the adjustment.

The Component. Event Class • A Component. Event is generated when the size, position,

The Component. Event Class • A Component. Event is generated when the size, position, or visibility of a component is changed. • It has following events defined by constants: COMPONENT_HIDDEN COMPONENT_MOVED COMPONENT_RESIZED COMPONENT_SHOWN • constructor: : The component was hidden. : The component was moved. : The component was resized. : The component became visible. Component. Event(Component src, int type) Here, src is a reference to the object that generated this event. The type of the event is specified by type. • Component. Event is the superclass either directly or indirectly of Container. Event, Focus. Event, Key. Event, Mouse. Event, and Window. Event.

 • The get. Component( ) method returns the component that generated the event.

• The get. Component( ) method returns the component that generated the event. • It is shown here: Component get. Component( )

The Container. Event Class • A Container. Event is generated when a component is

The Container. Event Class • A Container. Event is generated when a component is added to or removed from a container. • There are two types of container events. COMPONENT_ADDED: Says component added to container. COMPONENT_REMOVED: Component removed from container. • Constructor: Container. Event(Component src, int type, Component comp) • Methods: Container get. Container( ): obtains a reference to the container that generated this event. Component get. Child( ) : returns a reference to the component that was added to or removed from the container.

The Focus. Event Class • A Focus. Event is generated when a component gains

The Focus. Event Class • A Focus. Event is generated when a component gains or loses input focus. These events are FOCUS_GAINED and FOCUS_LOST. • It has these constructors: Focus. Event(Component src, int type) Focus. Event(Component src, int type, boolean temporary. Flag, Component other) • src- reference to the component that generated this event. • type - type of the event • temporary. Flag -set to true if the focus event is temporary. • Other- specifies focus of other component(opposite component)

 • Methods: Component get. Opposite. Component( ): determine the other component. boolean is.

• Methods: Component get. Opposite. Component( ): determine the other component. boolean is. Temporary( ) : indicates if this focus change is temporary. The method returns true if the change is temporary. Otherwise, it returns false.

The Input. Event Class • The abstract class Input. Event is a subclass of

The Input. Event Class • The abstract class Input. Event is a subclass of Component. Event and is the superclass for component input events. Its subclasses are Key. Event and Mouse. Event. • Input. Event class defined the following eight values to represent the modifiers: ALT_MASK ALT_GRAPH_MASK BUTTON 1_MASK BUTTON 2_MASK BUTTON 3_MASK CTRL_MASK META_MASK SHIFT_MASK • because of possible conflicts between the modifiers used by keyboard events and mouse events, and other issues, the following extended modifier values were added: ALT_DOWN_MASK BUTTON 2_DOWN_MASK META_DOWN_MASK ALT_GRAPH_DOWN_MASK BUTTON 3_DOWN_MASK SHIFT_DOWN_MASK BUTTON 1_DOWN_MASK CTRL_DOWN_MASK

The Key. Event Class • AKey. Event is generated when keyboard input occurs. •

The Key. Event Class • AKey. Event is generated when keyboard input occurs. • There are three types of key events, identified by these integer constants: KEY_PRESSED, KEY_RELEASED, and KEY_TYPED. • There are many other integer constants. • VK_0 -VK_9 : ASCII equivalents of the numbers • VK_A -VK_Z : letters. • VK_ALT VK_DOWN VK_LEFT VK_UP VK_RIGHT VK_CANCEL VK_ENTER VK_SHIFT VK_CONTROL VK_ESCAPE VK_PAGE_UP VK_PAGE_DOWN

Constructor: • Key. Event(Component src, int type, long when, int modifiers, int code, char

Constructor: • Key. Event(Component src, int type, long when, int modifiers, int code, char ch) Src-reference to component that generated the event. type- type of the event When- system time at which the key was pressed Modifiers- indicates which modifiers were pressed Code- virtual key code, such as VK_UP, VK_A Ch-character equivalent is passed in ch Else CHAR_UNDEFINED or VK_UNDEFINED is passed. char get. Key. Char( ): returns the character that was entered int get. Key. Code( ): returns the key code.

The Mouse. Event Class • There are eight types of mouse events MOUSE_CLICKED :

The Mouse. Event Class • There are eight types of mouse events MOUSE_CLICKED : The user clicked the mouse. MOUSE_DRAGGED : The user dragged the mouse. MOUSE_ENTERED : The mouse entered a component. MOUSE_EXITED : The mouse exited from a component. MOUSE_MOVED : The mouse moved. MOUSE_PRESSED : The mouse was pressed. MOUSE_RELEASED : The mouse was released. MOUSE_WHEEL : The mouse wheel was moved • Constructor : Mouse. Event(Component src, int type, long when, int modifiers, int x, int y, int clicks, boolean triggers. Popup)

Methods: • int get. X( ) & int get. Y( ) : return the

Methods: • int get. X( ) & int get. Y( ) : return the X and Y coordinates of the mouse within the component when the event occurred. • Point get. Point( ): obtain the coordinates of the mouse. • void translate. Point(int x, int y): changes the location of the event. • int get. Click. Count( ): obtains the number of mouse clicks for this event. • boolean is. Popup. Trigger( ): tests if this event causes a popup menu to appear on this platform. • int get. Button( ): returns a value that represents the button that caused the event. Possible values are NOBUTTON 1 BUTTON 2 BUTTON 3.

The Mouse. Wheel. Event Class • The Mouse. Wheel. Event class encapsulates a mouse

The Mouse. Wheel. Event Class • The Mouse. Wheel. Event class encapsulates a mouse wheel event. • It has two integer constants: WHEEL_BLOCK_SCROLL : A page-up or page-down scroll event WHEEL_UNIT_SCROLL : A line-up or line-down scroll event. • Mouse. Wheel. Event(Component src, int type, long when, int modifiers, int x, int y, int clicks, boolean triggers. Popup, int scroll. How, int amount, int count) • int get. Wheel. Rotation( ) : Obtain the number of rotational units. • int get. Scroll. Type( ) : To obtain the type of scroll. • int get. Scroll. Amount( ) : obtain the number of units to scroll.

Event Source Button Check box Choice List Menu Item checkable Scroll bar Text components

Event Source Button Check box Choice List Menu Item checkable Scroll bar Text components Window Sources of Events Description Generates action events when the button is pressed. Generates item events when the check box is selected or deselected. Generates item events when the choice is changed. Generates action events when an item is doubleclicked; generates item events when an item is selected or deselected. Generates action events when a menu item is selected; generates item events when a menu item is selected or deselected. Generates adjustment events when the scroll bar is manipulated. Generates text events when the user enters a character. Generates window events when a window is activated, closed, deactivated, deiconified, opened, or quit.

Event Listener Interfaces The Action. Listener Interface • It defines the action. Performed( )

Event Listener Interfaces The Action. Listener Interface • It defines the action. Performed( ) method that is invoked when an action event occurs. The Adjustment. Listener Interface • It defines the adjustment. Value. Changed( ) method that is invoked when an adjustment event occurs. The Component. Listener Interface • void component. Resized(Component. Event ce) • void component. Moved(Component. Event ce) • void component. Shown(Component. Event ce) • void component. Hidden(Component. Event ce)

The Container. Listener Interface • void component. Added(Container. Event ce) • void component. Removed(Container.

The Container. Listener Interface • void component. Added(Container. Event ce) • void component. Removed(Container. Event ce) The Focus. Listener Interface • void focus. Gained(Focus. Event fe) • void focus. Lost(Focus. Event fe) The Item. Listener Interface void item. State. Changed(Item. Event ie)

The Key. Listener Interface • void key. Pressed(Key. Event ke) • void key. Released(Key.

The Key. Listener Interface • void key. Pressed(Key. Event ke) • void key. Released(Key. Event ke) • void key. Typed(Key. Event ke) The Mouse. Listener Interface • void mouse. Clicked(Mouse. Event me) • void mouse. Entered(Mouse. Event me) • void mouse. Exited(Mouse. Event me) • void mouse. Pressed(Mouse. Event me) • void mouse. Released(Mouse. Event me)

The Mouse. Motion. Listener Interface • void mouse. Dragged(Mouse. Event me) • void mouse.

The Mouse. Motion. Listener Interface • void mouse. Dragged(Mouse. Event me) • void mouse. Moved(Mouse. Event me) The Mouse. Wheel. Listener Interface • void mouse. Wheel. Moved(Mouse. Wheel. Event mwe) The Text. Listener Interface • void text. Changed(Text. Event te) The Window. Focus. Listener Interface • void window. Gained. Focus(Window. Event we) • void window. Lost. Focus(Window. Event we)

The Window. Listener Interface • void window. Activated(Window. Event we) • void window. Closing(Window.

The Window. Listener Interface • void window. Activated(Window. Event we) • void window. Closing(Window. Event we) • void window. Deactivated(Window. Event we) • void window. Deiconified(Window. Event we) • void window. Iconified(Window. Event we) • void window. Opened(Window. Event we)

Using the Delegation Event Model • Just follow these two steps: 1. Implement the

Using the Delegation Event Model • Just follow these two steps: 1. Implement the appropriate interface in the listener so that it will receive the type of event desired. 2. Implement code to register and unregister (if necessary) the listener as a recipient for the event notifications.

Handling Mouse Events • To handle mouse events, you must implement the Mouse. Listener

Handling Mouse Events • To handle mouse events, you must implement the Mouse. Listener and Mouse. Motion. Listener or Mouse. Wheel. Listener interfaces. • C: Javajdk 1. 6. 0_12binMouse. Events. java

Handling Keyboard Events • To handle keyboard events you need to implement Key. Listener

Handling Keyboard Events • To handle keyboard events you need to implement Key. Listener interface. • C: UsersPradeepDesktopSimple. Key. java • If you want to handle the special keys, such as the arrow or function keys, you need to respond to them within the key. Pressed( ) handler. They are not available through key. Typed( ). • To identify the keys, you use their virtual key codes. • C: UsersPradeepDesktopKey. Events. java

Adapter Classes • An adapter class provides an empty implementation of all methods in

Adapter Classes • An adapter class provides an empty implementation of all methods in an event listener interface. • Adapter classes are useful when you want to receive and process only some of the events that are handled by a particular event listener interface. • C: Javajdk 1. 6. 0_12binAdapter. Demo. java

Inner Classes • C: Javajdk 1. 6. 0_12binMouse. Pressed. Demo. java • C: Javajdk

Inner Classes • C: Javajdk 1. 6. 0_12binMouse. Pressed. Demo. java • C: Javajdk 1. 6. 0_12binInner. Class. Demo 1. java