Event Handling

Download as pptx, pdf, or txt
Download as pptx, pdf, or txt
You are on page 1of 24

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 componentMoved(ComponentEvent e): Invoked


when the component's position changes.

 void componentResized(ComponentEvent e):


Invoked when the component's size changes.

 void componentShown(ComponentEvent e): Invoked


when the component has been made visible.
ITEMLISTENER INTERFACE
 The class which processes the ItemEvent
should implement this interface. The object
can be registered using the addItemListener()
method. When the action event occurs, that
object's itemStateChanged method is invoked.
 Interface declaration
public interface ItemListener extends
EventListenerInterface methods

void itemStateChanged(ItemEvent e):


Invoked when an item has been selected or
deselected by the user.
KEYLISTENER INTERFACE
 Interface methods
void keyPressed(KeyEvent e): Invoked when
a key has been pressed.

void keyReleased(KeyEvent e): Invoked


when a key has been released.

void keyTyped(KeyEvent e): Invoked when a


key has been typed.
MOUSELISTENER INTERFACE
 Interface methods
 void mouseClicked(MouseEvent e): Invoked when the mouse
button has been clicked (pressed and released) on a
component.

 void mouseEntered(MouseEvent e): Invoked when the mouse


enters a component.

 void mouseExited(MouseEvent e): Invoked when the mouse


exits a component.

 void mousePressed(MouseEvent e): Invoked when a mouse


button has been pressed on a component.

 void mouseReleased(MouseEvent e): Invoked when a mouse


button has been released on a component.
TEXTLISTENER INTERFACE
 The class which processes the TextEvent
should implement this interface.The object
of that class must be registered with a
component. The object can be registered
using the addTextListener() method.
 Interface declaration
public interface TextListener extends
EventListenerInterface methods

void textValueChanged(TextEvent e):


Invoked when the value of the text has
changed.
WINDOWLISTENER INTERFACE
Interface methods
 voidwindowActivated(WindowEvent e): Invoked when the Window is set to be the
active Window.

 void windowClosed(WindowEvent e): Invoked when a window has been closed as


the result of calling dispose on the window.

 void windowClosing(WindowEvent e): Invoked when the user attempts to close


the window from the window's system menu.

 void windowDeactivated(WindowEvent e): Invoked when a Window is no longer


the active Window.

 void windowDeiconified(WindowEvent e): Invoked when a window is changed


from a minimized to a normal state.

 void windowIconified(WindowEvent e): Invoked when a window is changed from a


normal to a minimized state.

 void windowOpened(WindowEvent e): Invoked the first time a window is made


visible.
ADJUSTMENTLISTENER INTERFACE
 The interfaceAdjustmentListener is used for
receiving adjustment events. The class that
process adjustment events needs to
implements this interface.
 Class declaration
public interface AdjustmentListener
extends EventListenerInterface methods

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.

 void mouseMoved(MouseEvent e): Invoked


when the mouse cursor has been moved onto
a component but no buttons have been
pushed.
FOCUSLISTENER INTERFACE
Interface methods
 void focusGained(FocusEvent e): Invoked
when a component gains the keyboard focus.

 void focusLost(FocusEvent e): Invoked when


a component loses the keyboard focus.
EVENT ADAPTERS
 Adapters are abstract classes for receiving
various events. The methods in these classes
are empty. These classes exists as
convenience for creating listener objects.
 Following is the list of commonly used
adapters while listening GUI events in SWING.

You might also like