0% found this document useful (0 votes)
12 views29 pages

Event Handling

The document outlines the delegation event model used in Java, where events are generated by a source and processed by registered listeners, improving efficiency compared to previous models. It details the types of events, event sources, event listeners, and specific event classes such as ActionEvent and MouseEvent. Additionally, it describes various listener interfaces that handle different types of events in a user interface.

Uploaded by

saikumarvenkat60
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
12 views29 pages

Event Handling

The document outlines the delegation event model used in Java, where events are generated by a source and processed by registered listeners, improving efficiency compared to previous models. It details the types of events, event sources, event listeners, and specific event classes such as ActionEvent and MouseEvent. Additionally, it describes various listener interfaces that handle different types of events in a user interface.

Uploaded by

saikumarvenkat60
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 29

EVENT HANDLING

RS NANDHINI
THE DELEGATION EVENT MODEL
• A source generates a event and sends it to one or more listeners.
• The listener simply waits until it receives an event.
• Once an event is received, the listener processes the event and returns.
• A user interface element is able to “delegate” the processing of an event to a
separate piece of code.
THE DELEGATION EVENT MODEL
• Listeners must register with a source in order to receive an event
notification.
• This provides important benefit: notifications are only sent to the listeners
they want to receive them.
• This is more efficient way to handle events than the design used in the
previous versions of JAVA.
• Previously, an event was propagated up the containment hierarchy until it
was handled by a component.
• This required components to receive events that they did not process, and it
was wasted valuable time.
• The delegation event model eliminates this overhead.
ADVANTAGE
• The application logic that processes events is cleanly separated from the
user interface logic that generates those events.
EVENTS
• An event is an object that describes the 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 keyword, selecting an item in a list, and
clicking the mouse.
EVENTS
• 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.
• You are free to define the events that are appropriate for your application.
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.
EVENT SOURCES
• Example: the method that registers a keyboard event listener is called
addKeyListener().
• The method that registers a mouse motion listener is called
addMouseMotionListener().
• When an event occurs, all registered listeners are notified and receive a copy of
the event object.
• This is knows as multicasting the event.
• In all cases, notifications are sent only to listeners that register to receive them.
EVENT SOURCES
• Some sources may allow only one listener to register.
• The general form of such a method is this:
• public void addTypeListener(TypeListener el)
throws java.util.TooManyListenerException
• Here, Type is the name of the event, and el is a reference to the event listener.
• When such an event occurs, the registered listener is notified.
• This is known as unicasting the event.
EVENT SOURCES
• 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.
• For example, to remove a keyboard listener, you would call
removeKeyListener().
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 type 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.
EVENT CLASSES
• The classes that represent events are at the core of Java’s event handling
mechanism.
• At the root of the Java event class hierarchy is EventObject, which is in java.util.
• It is the super class for all events. Its one constructor is shown here:
• EventObject(Object src)
• Here, src is the object that generates this event.
• EventObject contains two methods: getSource() and toString().
EVENT CLASS
• The class AWTEvent, defined within the java.awt package, is a subclass of
EventObject.
• It is the superclass(either directly or indirectly) of all AWT-based events used by
delegation event model.
• Its getID() method can be used to determine the type of the event
• int getID()
EVENT CLASS
• EventObject is a superclass of all events.
• AWTEvent is a superclass of all AWT events that are handled by the
delegation event model.
EVENT CLASS DESCRIPTION
ActionEvent Generated when a button is pressed, a lsit item is double-
clicked, or a menu item is selected.
AdjustmentEvent Generated when a scroll bar is manipulated.
ComponentEvent Generated when a component is hidden, moved, resized or
becomes visible.
ContainerEvent Generated when a component is added to or removed from a
container.
FocusEvent Generated when a component gains or loses keyboard focus.
InputEvent Abstract superclass for all component input event classes.
ItemEvent Generated when a check box or list item is clicked; also occurs
when a choice selection is made or a checkable menu item is
selected or deselected.
KeyEvent Generated when input is received from the keyboard.
MouseEvent Generated when the mouse is dragged, move, clicked, presses
or released; also generated when the mouse enters or exits a
component.
MouseWheelEvent Generated when the mouse wheel is moved.
TextEvent Generated when the value of a text area or text field is
changed.
WindowEvent Generated when a window is activated, closed, deactivated,
deiconified, iconified, opened or quit.
The MouseEvent Class

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.


THE MouseEvent Class
• MouseEvent is a subclass of InputEvent. One of its constructors are:
• MouseEvent(Component src, int type, long when, int modifiers,
int x, int y, int clicks, Boolean triggerPopup)
SOURCE OF EVENTS
Event Source Description
Button Generates action events when the button is pressed.
Checkbox Generates item events when the check box is selected or
deselected.

Choice Generates item events when the choice is changed.


List Generates action events when an item is double-clicked;
generates item events when an item is selected or deselected.

Menu item Generates action events when a menu item is selected;


generates item events when a checkable menu item is selected
or deselected.

Scrollbar Generates adjustment events when the scroll bar is


manipulated.

Text components Generates text events when the user enters a character.
Window Generates window events when a window is activated, closed,
deactivated, deiconified, iconified, opened, or quit.
EVENT LISTENER INTERFACES
Interface Description

ActionListener Defines one method to receive action events.

Defines one method to recognize when the state of an


ItemListener item changes.

KeyListener Defines three methods to recognize when a key is


pressed, released, or typed.

FocusListener Defines two methods to recognize when a component


gains or loses focus.

ContainerListener Defines two methods to recognize when a component is


added to or removed from a container.

ComponentListener Defines four methods to recognize when a component is


hidden, moved, resized, or shown.
Interface Description

AdjustmentListener Defines one method to receive adjustment events.

Defines five methods to recognize when the mouse is


MouseListener clicked, enters a component, exits a component, is
pressed, or is released.

Defines two methods to recognize when the mouse is


MouseMotionListener
dragged or moved.

Defines one method to recognize when the mouse wheel


MouseWheelListener is moved.

Defines one method to recognize when a text value


TextListener changes.

Defines two methods to recognize when a window gains


WindowFocusListener or loses focus.
Defines seven methods to recognize when a window is
WindowListener activated, closed, deactivated, deiconified, iconified,
opened, or quit.
HANDLING MOUSE EVENTS
HANDLING KEYBOARD EVENTS

You might also like