Chapter03 Event
Chapter03 Event
(22517)
Event Handling
12 Marks
Specific Objectives
2
The Delegation Event Model
Java adopts the so-called "Event-Driven" (or "Event-
Delegation") programming model for event-handling
Provides standard mechanism for a source to generate an
event and send it to a set of listeners
The listener simply waits until it receives an event.
Once received, the listener processes the event and then
return.
The AWT's event-handling classes are kept in package
java.awt.event.
3
There are mainly three parts in delegation event
model:
- Event Source – the class from which events are
generated when some change in the property of the
component
- Event Listeners – the classes which receive notifications
of events
- Event– defines the change in state in the event source
class.
Event
7
Event Source
Event Source is an object that generates an event.
This occurs when the internal state of that object
changes.
Sources may generate more than one type of
event.
Sources must register listener so that listener will
receive notifications.
For Register/add:
public void addTypeListener(TypeListener el)
For remove:
public void removeTypeListener(TypeListener el)
8
Event Listener
9
Event Classes
Event Classes are core of Java’s event handling
mechanism.
EventObject is the root of the Java event class
hierarchy .
EventObject is present in java.util.
EventObject is the superclass for all events.
Constructor: EventObject(Object src).
EventObject class has defines two methods:
Object getSource( ) : method returns the source of
the event.
String toString( ) : returns string equivalent of the
event. 10
Event Classes : AWTEvent
Summarize:
EventObject is a superclass of all events.
AWTEvent is a superclass of all AWT events that are 11
To Summarize:
EventObject is a superclass of all events.
12
Summary
Event Source – Event Source is an object that
generates an event.
src: object which generate event
type: type of event
cmd: Command string
modifiers: which modifier key
when: when the event occurred
18
ActionEvent
getActionCommand() used to get command name.
int getModifiers() used to get modifier key.
long getWhen( ) used to get when event is
generated.
19
ItemEvent Class
ItemEvent is generated when a check box or a list
item is clicked or when a checkable menu item is
selected or deselected.
Item events:
DESELECTED The user deselected an item.
SELECTED The user selected an item.
ITEM_STATE_CHANGED that signifies a change of state.
Constructor:
ItemEvent(ItemSelectable src, int type, Object entry, int
state)
20
ItemEvent Class
Constructor:
ItemEvent(ItemSelectable src, int type, Object item, int state)
src
The object that generated the event.
type
The type ID of the event.
item
The item whose state is changing.
state
Either SELECTED or DESELECTED
21
ComponentEvent class
A ComponentEvent is generated when the size,
position, or visibility of a component is changed.
There are four types of component events
COMPONENT_HIDDEN The component was hidden.
COMPONENT_MOVED The component was moved.
COMPONENT_RESIZED The component was resized.
COMPONENT_SHOWN The component became
visible.
Constructor:
ComponentEvent(Component src, int type)
22
ContainerEvent class
ContainerEvent is generated when a component
is added to or removed from a container.
Two Constants defined
COMPONENT_ADDED and
COMPONENT_REMOVED
Subclass of ComponentEvent Class
Constructor:
ContainerEvent(Component src, int type, Component
comp)
23
FocusEvent class
FocusEvent is generated when a component gains or
loses input focus.
Two constants defined:
FOCUS_GAINED and FOCUS_LOST.
Subclass of ComponentEvent Class
Constructors:
FocusEvent(Component src, int type)
FocusEvent(Component src, int type, boolean
temporaryFlag)
Focus Event(Component src, int type, boolean
temporaryFlag, Component other)
isTemporary( ) method indicates if this focus change is 24
KeyEvent Class
KeyEvent is generated when keyboard input occurs.
There are three types of key events:
KEY_PRESSED,
KEY_RELEASED, and
KEY_TYPED
Constructor:
KeyEvent(Component src, int type, long when, int modifiers,
int code)
KeyEvent(Component src, int type, long when, int modifiers,
int code, char ch)
25
KeyEvent Class
There are many other integer constants that are defined
by KeyEvent.
26
MouseEvent Class
Eight types of mouse events.
Direct subclass of InputEvent Class and indirect
subclass of ComponentEvent Class
The MouseEvent class defines the following integer
constants
MOUSE_CLICKED The user clicked the mouse.
MOUSE_ENTERED The mouse entered a component.
MOUSE_PRESSED The mouse was pressed.
MOUSE_RELEASED The mouse was released.
MOUSE_EXITED The mouse exited from a component.
MOUSE_MOVED The mouse moved.
MOUSE_DRAGGED The user dragged the mouse.
MOUSE_WHEEL The mouse wheel was moved
27
MouseEvent Class
MouseEvent is a subclass of InputEvent.
Constructor:
MouseEvent(Component src, int type, long when, int
modifiers, int x, int y, int clicks, boolean triggersPopup)
28
TextEvent Class
These are generated by text fields and text areas when
characters are entered by a user or program.
TextEvent defines the integer constant
TEXT_VALUE_CHANGED.
Constructor:
TextEvent(Object src, int type)
29
WindowEvent Class
There are ten types of window events.
Subclass of ComponentEvent
WindowEvent class defines integer constants:
WINDOW_ACTIVATED The window was activated.
WINDOW_CLOSED The window has been closed.
WINDOW_CLOSING The user requested that the window be closed.
WINDOW_DEACTIVATED The window was deactivated.
WINDOW_DEICONIFIED The window deiconified (min => Normal).
WINDOW_GAINED_FOCUS The window gained input focus.
WINDOW_ICONIFIED The window was iconified(Normal=>min)
WINDOW_LOST_FOCUS The window lost input focus.
WINDOW_OPENED The window was opened.
WINDOW_STATE_CHANGED The state of the window changed.
30
WindowEvent Class
WindowEvent is a subclass of ComponentEvent.
WindowEvent(Window src, int type, Window other)
WindowEvent(Window src, int type, int fromState, int toState)
WindowEvent(Window src, int type, Window other, int
fromState, int toState)
31
AdjustmentEvent Class
AdjustmentEvent is a subclass of AWTEvent.
Constructor
AdjustmentEvent ( Adjustable source, int id, int type,
int value)
AdjustmentEvent ( Adjustable source, int id, int type,
int value, boolean isAdjusting)
32
AdjustmentEvent Class
ADJUSTMENT_FIRST
Marks the first integer id for the range of adjustment event ids.
ADJUSTMENT_LAST
Marks the last integer id for the range of adjustment event ids.
ADJUSTMENT_VALUE_CHANGED
The adjustment value changed event.
BLOCK_DECREMENT
The block decrement adjustment type.
BLOCK_INCREMENT
The block increment adjustment type.
TRACK
The absolute tracking adjustment type.
UNIT_DECREMENT
The unit decrement adjustment type.
UNIT_INCREMENT
The unit increment adjustment type.
33
AdjustmentEvent class
Methods
Object getAdjustable ()
Returns the Adjustable object where this event originated.
int getAdjustmentType ()
Returns the type of adjustment which caused the value changed
event.
int getValue ()
Returns the current value in the adjustment event.
boolean getValuesAdjusting ()
Returns true if this is one of multiple adjustment events.
String paramString ()
Returns a string representing the state of this Event
Adapter Class
An adapter class provides an empty implementation
of all methods in an event listener interface.
Adapter classes are useful when you want to receive and
process only some of the events that are handled by a
particular event listener interface.
36
Adapter Class : Different Classes
ComponentAdapter ComponentListener
ContainerAdapter ContainerListener
FocusAdapter FocusListener
KeyAdapter KeyListener
MouseAdapter MouseListener
MouseMotionAdapter MouseMotionListener
WindowAdapter WindowListener
37
Inner Class
Inner class is class which defined in another class.
In inner classes, the Adapter class will defined in same
class.
No need of passing reference of object as it is in same
scope.
Ex.
38
Anonymous Inner Class
An anonymous inner class is one that is not assigned a
name.
39
Event Listeners Interfaces
Event Delegation Model has two parts: Sources and
Listeners.
When event generated, then event source invoked
appropriate method defined by interface.
40
Action Listener Interface
41
ComponentListener Interface
Defines four methods to recognize when a component is
hidden, moved, resized, or shown.
void componentResized(ComponentEvent ce)
void componentMoved(ComponentEvent ce)
void componentShown(ComponentEvent ce)
void componentHidden(ComponentEvent ce)
42
ContainerListener Interface
Defines two methods to recognize when a component is
added to or removed from a container.
void componentAdded(ContainerEvent ce)
void componentRemoved(ContainerEvent ce)
43
FocusListener Interface
Defines two methods to recognize when a component
gains or loses keyboard focus.
void focusGained(FocusEvent fe)
void focusLost(FocusEvent fe)
44
ItemListener Interface
Defines one method to recognize when the state of an
item changes.
void itemStateChanged(ItemEvent ie)
45
KeyListener Interface
Defines three methods to recognize when a key is
pressed, released, or typed.
void keyPressed(KeyEvent ke)
void keyReleased(KeyEvent ke)
void keyTyped(KeyEvent ke)
46
MouseListener Interface
Defines five methods to recognize when the mouse is
clicked, enters a component, exits a component, is
pressed, or is released.
void mouseClicked(MouseEvent me)
void mouseEntered(MouseEvent me)
void mouseExited(MouseEvent me)
void mousePressed(MouseEvent me)
void mouseReleased(MouseEvent me)
47
MouseMotionListener Interface
Defines two methods to recognize when the mouse is
dragged or moved.
void mouseDragged(MouseEvent me)
void mouseMoved(MouseEvent me)
48
TextListener Interface
Defines one method to recognize when a text value
changes.
void textValueChanged(TextEvent te)
49
WindowFocusListener Interface
Defines two methods to recognize when a window gains
or loses input focus
void windowGainedFocus(WindowEvent we)
void windowLostFocus(WindowEvent we)
50
WindowListener Interface
Defines seven methods to recognize:
void windowActivated(WindowEvent we)
void windowClosed(WindowEvent we)
void windowClosing(WindowEvent we)
void windowDeactivated(WindowEvent we)
void windowDeiconified(WindowEvent we)
void windowIconified(WindowEvent we)
void windowOpened(WindowEvent we)
51
AdjustmentListener Interface
Defines one methods to recognize when the value of the
adjustable has changed :
void adjustmentValueChanged(AdjustmentEvent e)
52