Event Handling
Event Handling
Event Handling
WHAT IS AN EVENT?
Change in the state of an object is known as
event i.e. event describes the change in state
of source. Events are generated as result of
user interaction with the graphical user
interface components.
For example, clicking on a button, moving
the mouse, entering a character through
keyboard, selecting an item from list,
scrolling the page are the activities that
causes an event to happen.
TYPES OF EVENT
Foreground Events - Those events which
require the direct interaction of user. For
example, clicking on a button, moving the
mouse, entering a character through
keyboard,selecting an item from list,
scrolling the page etc.
Background Events - Those events that
require the interaction of end user are
known as background events. Operating
system interrupts, hardware or software
failure, timer expires, an operation
completion are the example of background
events.
WHAT IS EVENT HANDLING?
Event Handling is the mechanism that controls
the event and decides what should happen if
an event occurs. This mechanism have the
code which is known as event handler that is
executed when an event occurs.
Java Uses the Delegation Event Model to
handle the events. This model defines the
standard mechanism to generate and handle
the events.
The Delegation Event Model has the
following key participants namely:
Source - The source is an object on which event
occurs. Source is responsible for providing
information of the occurred event to it's handler.
Java provide as with classes for source object.
Listener - It is also known as event handler.
Listener is responsible for generating response to
an event. From java implementation point of
view the listener is also an object. Listener waits
until it receives an event. Once the event is
received , the listener process the event an then
returns.
STEPS INVOLVED IN EVENT HANDLING
The User clicks the button and the event is
generated.
Now the object of concerned event class is
created automatically and information about
the source and the event get populated with
in same object.
Event object is forwarded to the method of
registered listener class.
the method is now get executed and returns.
EVENT CLASSES
The Event classes represent the event. Java
provides us various Event classes but we will
discuss those which are more frequently used.
Following is the list of commonly used event
classes.
EVENT LISTENERS INTERFACES
The Event listener represent the interfaces
responsible to handle events. Java provides us
various Event listener classes but we will discuss
those which are more frequently used. Every
method of an event listener method has a single
argument as an object which is subclass of
EventObject class. For example, mouse event
listener methods will accept instance of
MouseEvent, where MouseEvent derives from
EventObject.
Following is the list of commonly used event
listeners.
ACTIONLISTENER INTERFACE
The class which processes the ActionEvent
should implement this interface.The object of
that class must be registered with a
component. The object can be registered
using the addActionListener() method. When
the action event occurs, that object's
actionPerformed method is invoked.
Interface declaration
public interface ActionListener extends
EventListenerInterface methods
actionPerformed(ActionEvent e): Invoked
when an action occurs.
COMPONENTLISTENER INTERFACE
Interface methods
void componentHidden(ComponentEvent e):
Invoked when the component has been made
invisible.
void adjustmentValueChanged
(AdjustmentEvent e): Invoked when the
value of the adjustable has changed.
CONTAINERLISTENER INTERFACE
Interface methods
void componentAdded(ContainerEvent e):
Invoked when a component has been added
to the container.
void componentRemoved(ContainerEvent
e): Invoked when a component has been
removed from the container.
MOUSEMOTIONLISTENER INTERFACE
Interface methods
void mouseDragged(MouseEvent e): Invoked
when a mouse button is pressed on a
component and then dragged.