Event Handling
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
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