0% found this document useful (0 votes)
9 views31 pages

Ajp 3 PPT

ajp

Uploaded by

towaxa1167
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
9 views31 pages

Ajp 3 PPT

ajp

Uploaded by

towaxa1167
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 31

Event Handling

By
Sangita B.Chavan
Lecturer in Computer Technology
Govt. Polytechnic,Ahmednagar
Learning Objective
 Use delegation event model to develop
event driven program for the given
problem.
Handling Events:-
 Every GUI applet/application is event-driven.
 Every movement of the mouse,keypress,button click
generates an event.
 In java,events are represented by objects that
encapsulate all the information about an event.
 The superclass of all events is
java.util.EventObject,which is a subclass of
object class.all awt events are supported by
java.awt.event package having AWTEvent class
on the top of the hierarchy.
Event Delegation Model:-
 Event handling in java is based on the Event Delegation
Model which defines standard and consistent
mechanism to generate and process events.
 The principal elements of Event Delegation Model are:
1.Event- is an object that describes a state change in a
source.
 it can be generated by user interaction with GUI
controls.
e.g-pressing a button,clicking a checkbox.

2.Event Source-is an object that generates an


event.Source may generate more than one type of
event.
 A source must register listener in order to receive
notifications about specific type of event,by using the
following method:
Public void addTypeListener(TypeListener l)

e.g-addKeyListener(),addMouseMotionListener()
 A source may unregister the listener in a specific
type of event as
Public void
removeTypeListener(TypeListener l)

3.Event Listener-is an object that is notified


when an event occurs.
e.g MouseListener ,keyListener
Note:Listener must have been registered with one
or more sources to receive notifications
EVENT CLASSES:-
Event classes:-

AWTEvent classes are divided are int two main categories:


1.High level events:-these classes represent user interaction
with a GUI component like button textfield etc.includes
ActionEvent,AdjustmentEvent,ItemEvent and
TextEvent..
2.Low level events:-represent low-level input or window
events.KeyEvent,PaintEvent,MouseEvent,
WindowEvent etc.
ActionEvent:-
 An Action Event is generated when a button is
pressed,a list item is double-clicked,or a menu item
is selected.
 The ActionEvent class defines an integer
constant,ACTION_PERFORMED,which can be
used to identify an action event.
 It has following constructor:
ActionEvent(Object source,int type,String
cmdstr,int modifiers)
 The ActionEvent class also provides the
following useful methods:
 String getActionCommand():Returns
the Label/command of source Object.
 int getModifiers(): Returns the integer
constant value of the modifier which was
used.
ItemEvent:-
 An ItemEvent is generated when a check box or a list item is
clicked or when a checkable menu item is selected or
deselected.
 There are two types of item events:
 1) DESELECTED: When the user deselected an item.
 2)SELECTED: When the user selected an item.
 ItemEvent refers to a change of state that is defined by an
integer constant ITEM_STATE_CHANGED.
ItemEvent(ItemSelected source,int type,object item,int
state)
 The type of the item event is defined by two integer constants
DESELECTED and SELECTED.
 The ItemEvent class provides methods:
Object getItem()
int getStateChange()
ItemSelectable getItemSelectable()
TextEvent :-
 TextEvent are generated by text fields and
text areas when characters are entered
by a user or program.
 This event is defined by an integer
constant TEXT_VALUE_CHANGED.
 The textfield and textareas generate this
event when characters are entered by a
user.
TextEvent(Object source,int type)
MouseEvent:-
 This event is generated when the user moves the
mouse or presses a mouse button.
 The exact action is
MOUSE_CLICKED.MOUSE_DRAGGED,MOUSE_E
NTERED,MOUSE_MOVED,
MOUSE_EXITED,MOUSE_PRESSED or
MOUSE_RELEASED.
The MouseEvent class also defines some methods:
int getX(), int getY(): return the X and Y
coordinates of the mouse when the event occurred.
int getClickCount()
KeyEvent:-
 This event is generated by the keyboard inputs,i.e
when key is pressed or released.the constants
KEY_PRESSED,KEY_RELEASED,and
KEY_TYPED .
 KEY are defined as virtual key codes and are
independent of any modifiers such as shift,control
or alt.some are VK-0 to VK_9,VK_A to
VK_Z,VK_ENTER,VK_UP,VK_SHIFT,VK_CONT
ROL,VK_ALT,etc
 KeyEvent class some methods-
char getKeyChar():returns the character that
was presses/entered/typed.
int getKeyCode() : returns the key code of
the key which was presses/entered/typed.
WindowEvent:-
 This event is generated when some operation is
performed on a window.WindowEvent class
include
WINDOW_OPENED,WINDOW_CLOSING,
WINDOW_CLOSED,WINDOW_ICONIFIED,
WINDOW_DEICONIFIED,WINDOW_ACTIV
ATED and WINDOW_DEACTIVATED.
 This class provides a method which returns the
window object that caused the event to be
generated.
Window getWindow() method returns the
window object that generated the event.
Event Classes:
Event Classes Description

ActionEvent Generated when mouse button is pressed,MenuItem is


selected or List item is double clicked.
ItemEvent Generated when a check box or a list item is clicked or
CheckaboxMenu Item is checked or unchecked.
KeyEvent Generated when keyboard key is pressed.
MouseEvent Generated when the mouse is
dragged,moved,clicked,pressed,released,enters or exits
from a component object.
TextEvent Generated when the value of a TextArea or TextField is
changed.
WindowEvent Generated when a window is
activated,deactivated,closed,opened,iconified,deiconified
or about to be closed.
Event sources:
Source Event

Button Generates event when the button is


pressed.
Checkbox Generates event when checkbox is checked
or unchecked.
Choice Generates event when a choice is changed.
List Generates event when an item is selected.
MenuItem Generates event when a MenuItem is
selected.
Checkbox Generates event when a
MenuItem CheckboxMenuItem is checked or
unchecked
Source Event

TextArea,TextFi Generates event when text is typed in it.


eld
Window Frame Generates event when a window is
minimized,maximized,activated,deactivate
d,Iconified,deiconified,opened or closed.

Scrollbar, Generates event when a scrollbar is


ScrollPane moved.
Summary
 Use delegation event model to develop
event driven program for the given
problem.
 Use relevant AWT/Swing components to
handle the given event.
Event Handling
By
Sangita B.Chavan
Lecturer in Computer Technology
Govt. Polytechnic,Ahmednagar
Learning Objective
 Use delegation event model to develop
event driven program for the given
problem.
Event Listener interfaces:-
 In Event delegation model ,an event source
informs event listeners about events when an
event occurs and passes the necessary
information about the event.
 Event listeners contains the code which is to be
performed when certain type of event occurs.
 To handle the specific event,the corresponding
interface has to be implemented and its object
need to be attached to the particular event
source.
ActionListener:-
 This interface defines a method
public void
actionPerformed(ActionEvent e)
 Which is executed when an ActionEvent is
generated from a source.
 object getSource():Returns the object on
which the event occurred
ItemListener:-
The following method if this listener is
executed when the state of an item changes.
 public void
itemStateChanged(ItemEvent e)
AdjustmentListener:-
 The adjustmentValueChanged() method of
this listener is executed when an adjustment
event occurs.
 Its methods is as follows:
public void
adjustmentValueChanged(AdjustmentEvent e)
KeyListener:-
 The methods of this interface are invoked
when a key is pressed or released or
when a character has been entered.
 It defines the following methods:
void keyPressed(KeyEvent e)
void keyReleased(KeyEvent e)
void keyTyped(KeyEvent e)
MouseListener:-
 These are generated whenever a mouse is
moved,clicked,pressed,released,etc.
 The MouseListener interface defines the following
five methods:
void mouseClicked(MouseEvent e)
void mouseEntered(MouseEvent e)
void mouseExited(MouseEvent e)
void mousePressed(MouseEvent e)
void mouseReleased(MouseEvent e)
 The mouseClicked() method is executed when
the mouse is pressed and released at the same point.
MouseMotionListener:-
 This interface defines two methods,which
are executed multiple times as the mouse
is moved or dragged.
void mouseDragged(MouseEvent e)
void mouseMoved(MouseEvent e)
TextListener:-
 This interface defines the
textValueChanged() method that is
invoked when a change occurs in a text
area or text field.
 void textValueChanged(TextEvent
e):
Invoked when the value of the text has
changed.
WindowListener:-
 This interface defines the following methods:
void windowActivated(WindowEvent e)
void windowClosed(WindowEvent e)
void windowClosing(WindowEvent e)
void windowDeactivated(WindowEvent e)
void windowDeiconified(WindowEvent e)
void windowIconified(WindowEvent e)
void windowOpened(WindowEvent e)
 This windowClosing() method is called when a window is being
closed,i.e.when the cross(X) button of the title bar of frame or
dialog window is clicked.
Adapter classes:-
 Adapter classes are used to simplify the
creation of event handlers.
 They provide empty implementation of all
methods associated with low-level listeners.
 They are useful when you want to receive and
process only some of the events that are
handled by a particular listener interface.
 Adapter classes with their corresponding
listener interfaces.
Adapter classes:-
Adapter class Listener interface

WindowAdapter WindowListener

KeyAdapter KeyListener

MouseAdapter MouseListener

MouseMotionAdapter MouseMotionListener

FocusAdapter FocusListener

ComponentAdapter ComponentListener

You might also like