Shri Chhatrapati Shivaji Maharaj College
of Engineering Nepti Ahmednagar
Subject :- AJP
Guide :- Prof.S.V.Chitale
Topic Name :- Event Handling
Name Endrollment No
Labade Nitin Shravani 2219530051
Kanase Gayatri Audumbar 2219530046
Warule Swamini Vijay 2219530065
Shirsath Samruddhi Suyog 2219530061
Changan Chaitali Popat 2219530035
Pokale Ishwari Ravindra 2219530056
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.
The event object is used to carry the required information about the state
change.
The java.awt.event package can be used to provide various event classes
Types of Event
Foreground Events
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.
Important Event Classes and Interface in Java
Classes defined in java.awt.event package:
Event Classes Description Listener Interface
Action Event When a button is clicked or a list item is ActionListene
doubleclicked, an ActionEvent is triggered.
MouseEvent This event indicates a mouse action occurred Mouse Listener
in a component.
KeyEvent The Key event is triggered when the KeyListener
character is entered using the keyboard
ItemEvent An event that indicates whether an item was ItemListener
selected or not.
TextEvent when the value of a text area or text field is TextListener
changed
WindowEvent The object of this class represents the change WindowListener
in the state of a window and are generated
when the window is activated, deactivated,
deiconified, iconified, opened or closed
ComponentEvent when a component is hidden, moved, resized, ComponentEventLister
or made visible
ContainerEvent when a component is added or removed from ContainerListener
a container
AdjustmentEvent when the scroll bar is manipulated AdjustmentListener
FocusEvent when a component gains or loses FocusListener
keyboard focus
ActionEvent
Class constructors :
public ActionEvent(Object source, int id, String command, long when, int
modifiers)
Parameters:
source - The object that originated the event
id - An integer that identifies the event.
command - A string that may specify a command (possibly one of several)
associated with the event
modifiers - The modifier keys down during event (shift, ctrl, alt, meta).
Passing negative parameter is not recommended. Zero value means that no
modifiers were passed. when - A long that gives the time the event occurred.
Methods
public String getActionCommand()
Returns the string identifying the command for this event.
public long getWhen()
Returns the time at which the event took place. This is called
the events timestamp.
public int getModifiers()
Returns the modifier keys held down during this action event.
For example, when a button is pressed, an action event is
generated. The getModifiers()method returns a value that
indicates which modifier keys (ALT, CTRL, META, and/or SHIFT)
were pressed when the event was generated.
ItemEvent
Constructor
ItemEvent(ItemSelectable source, int id, Object item, int stateChange)
Constructs an ItemEvent object.
Parameters:
source - The ItemSelectable object that originated the event
id - The integer that identifies the event type.
item - the item affected by the event
stateChange - An integer that indicates whether the item was selected or deselected.
The stateChange of any ItemEvent instance takes one of the following values:
ItemEvent.SELECTED :- The user selected an item.
ItemEvent.DESELECTED :- The user deselected an item.
In addition, ItemEvent defines one integer constant, ITEM_STATE_CHANGED, that
signifies a
change of state.
Metho
ds
getItemSelectable
public ItemSelectable getItemSelectable()
Returns the ItemSelectable object that originated the event.
Lists and choices are examples of user interface elements that implement the
ItemSelectable interface
getItem
public Object getItem()
Returns: the item (object) that was affected by the event
getStateChange
public int getStateChange()
Returns: an integer that indicates whether the item was selected or
deselected
paramString
public String paramString()
Returns a parameter string identifying this item event.
Interface:- ItemListener This interface deals with the item event. Following
is the event handling method available in the ItemListener interface:
void itemStateChanged(ItemEvent ie)
KeyEvent
Constructor
KeyEvent(Component source, int id, long when, int modifiers, int keyCode,
char keyChar)
Parameters:
source - The Component that originated the event
id - An integer indicating the type of event.
when - A long integer that specifies the time the event occurred.
modifiers - The modifier keys down during event (shift, ctrl, alt, meta).
keyCode - The integer code for an actual key, or VK_UNDEFINED (for a key-
typed
event)
keyChar - The Unicode character generated by this event, or
CHAR_UNDEFINED (for
key-pressed and key-released events which do not map to a valid Unicode
character)
KEY_PRESSED : generated when any key is pressed
KEY_RELEASED :- when any key is released
KEY_TYPED :- occurs only if a valid Unicode character could be generated.
Method
s int getKeyCode()
public
Returns the integer keyCode associated with the key in this event.
When a KEY_TYPED event occurs, getKeyCode( ) returns VK_UNDEFINED.
public char getKeyChar()
Returns the character associated with the key in this event. For example, the
KEY_TYPED event
for shift + "a" returns the value for "A".
If no valid character is available, then getKeyChar( ) returns CHAR_UNDEFINED.
Public abstract void keyPressed(KeyEvent e);
It is invoked when a key has been pressed
Public abstract void keyReleased(KeyEvent e);
It is invoked when a key has been pressed
Public abstract void keyTyped(KeyEvent e);
It is invoked when a key has been typed
Interface:-
KeyListener
This interface deals with the key events. Following are the event handling methods
available
the KeyListener interface
TextEvent
The object of this class represents the text events. The TextEvent is generated when character is
entered in the text fields or text area. The TextEvent instance does not include the characters
currently in the text component that generated the event rather we are provided with other
methods to retrieve that information.
Constructor
public TextEvent(Object source, int id)
Constructs a TextEvent object.
Parameters:
source- The (TextComponent) object that originated the event
id - An integer that identifies the event type
TextEvent defines the integer constant TEXT_VALUE_CHANGED.
Interface
TextListener
This interface deals with the text events. Following is the event handling method
available in the
TextListener interface:
void textValueChanged(TextEvent te)
MouseEvent
This event indicates a mouse action occurred in a component. This low-level event is generated
by
a component object for Mouse Events and Mouse motion events.
Constructor
public MouseEvent(Component source, int id, long when, int modifiers, int x, int y, int
clickCount, boolean popupTrigger, int button)
Parameters:
source - the Component that originated the event
id - the integer that identifies the event
when - a long int that gives the time the event occurred
modifiers - the modifier keys down during event (e.g. shift, ctrl, alt, meta)
x - the horizontal x coordinate for the mouse location
y - the vertical y coordinate for the mouse location
clickCount - the number of mouse clicks associated with event
popupTrigger - a boolean, true if this event is a trigger for a popup menu
button - which of the mouse buttons has changed state.
MouseEvents
MOUSE_PRESSED The mouse was pressed.
MOUSE_RELEASED The mouse was released.
MOUSE_CLICKED The user clicked the mouse (pressed and released)
MOUSE_ENTERED The mouse entered a component.
MOUSE_EXITED The mouse exited from a component.
Mouse Motion Events
MOUSE_DRAGGED The user dragged the mouse.
MOUSE_MOVED The mouse moved.
Methods
int getButton()
Returns which of the mouse buttons has changed state. The return value will be
one of these constants defined by MouseEvent. NOBUTTON BUTTON1 BUTTON2
BUTTON3 The NOBUTTON value indicates that no button was pressed or
released
int getClickCount()
Returns the number of mouse clicks associated with this event
Point getPoint()
Returns the x,y position of the event relative to the source component.
int getX()
Returns the horizontal x position of the event relative to the source component.
int getY()
Returns the vertical y position of the event relative to the source component
Interface:-
MouseListener
This interface deals with five of the mouse events. Following are the event
handling methods available in the MouseListener interface:
void mouseClicked(MouseEvent me)
void mousePressed(MouseEvent me)
void mouseReleased(MouseEvent me)
void mouseEntered(MouseEvent me)
void mouseExited(MouseEvent me)
MouseMotionListener
This interface deals with two of the mouse events. Following are the event
handling methods available in the MouseMotionListener interface:
void mouseMoved(MouseEvent me)
void mouseDragged(MouseEvent me)
Adapter classes
• The listener class that implements the Listener interface must provide
bodies for all of the methods of that interface.
• It is not a problem for all the semantic listener interfaces such as
ActionEvent, ItemEvent, TextEvent, AdapterEvent as each of them declares
only one method.
• However, for all the low-level listener interfaces where each interface
contains multiple methods, implementing each method can be somewhat
tedious, especially when we have to define methods in which we are not
interested.
• For example: Suppose we are interested in setting up only one listener
interface method windowClosing() of the WindowListener interface that
causes the program to terminate. In that case, we would not only need to
Adapter Classes Listener interface
WindowAdapter WindowListener
KeyAdapter KeyLisener
MouseAdapter MouseListener
MouseMotionAdapter MouseMotionListener
FocusAdapter FocusListener
ComponentAdapter ComponentListener