Java Programming :Event Handling(Types of Events)

2,964 views 22 slides Apr 30, 2024
Slide 1
Slide 1 of 22
Slide 1
1
Slide 2
2
Slide 3
3
Slide 4
4
Slide 5
5
Slide 6
6
Slide 7
7
Slide 8
8
Slide 9
9
Slide 10
10
Slide 11
11
Slide 12
12
Slide 13
13
Slide 14
14
Slide 15
15
Slide 16
16
Slide 17
17
Slide 18
18
Slide 19
19
Slide 20
20
Slide 21
21
Slide 22
22

About This Presentation

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.


Slide Content

Event Handling
SimmiS
Department of Computer Science(UG)
KristuJayantiCollege ,Bangalore

EventhandlingisfundamentaltoJavaprogramming because
itisusedtocreateeventdrivenprogramseg
Applets
GUIbasedwindowsapplication
WebApplication
Eventhandlingmechanism havebeenchanged
significantlybetweentheoriginalversionofJava(1.0)andall
subsequentversionsofJava,beginningwithversion1.1.
Themodernapproachtohandlingeventsisbasedon the
delegationeventmodel,
EventHandling

What is an Event?
Changeinthestateofanobjectisknownaseventi.e.eventdescribesthe
changeinstateofsource.
Eventsaregeneratedasresultofuserinteractionwiththegraphicaluser
interfacecomponents.
Forexample,clickingonabutton,movingthemouse,enteringacharacter
throughkeyboard,selectinganitemfromlist,scrollingthepagearethe
activitiesthatcausesaneventtohappen.

TypesofEvent
Theeventscanbebroadlyclassifiedintotwocategories:
ForegroundEvents-Thoseeventswhichrequirethedirectinteractionofuser.
Theyaregeneratedasconsequencesofapersoninteractingwiththegraphical
componentsinGraphicalUserInterface.
Forexample,clickingonabutton,movingthemouse,enteringacharacterthrough
keyboard,selectinganitemfromlist,scrollingthepageetc.
BackgroundEvents-Thoseeventsthatrequiretheinteractionofenduserareknownas
backgroundevents.
Operatingsysteminterrupts,hardwareorsoftwarefailure,timerexpires,anoperation
completionaretheexampleofbackgroundevents.

EventHandlingisthemechanismthatcontrolstheeventanddecideswhat
shouldhappenifaneventoccurs.
Thismechanismhavethecodewhichisknownaseventhandlerthatis
executedwhenaneventoccurs.
JavaUsestheDelegationEventModeltohandletheevents.
Thismodeldefinesthestandardmechanismtogenerateandhandlethe
events.Let'shaveabriefintroductiontothismodel.
WhatisEventHandling?

Source & Listener
TheDelegationEventModelhasthefollowingkeyparticipantsnamely:
Source-Thesourceisanobjectonwhicheventoccurs.
Sourceisresponsibleforprovidinginformationoftheoccurred
eventtoit'shandler.Javaprovidesclassesforsourceobject.
Listener-Itisalsoknownaseventhandler.
Listenerisresponsibleforgeneratingresponsetoanevent.From
javaimplementationpointofviewthelistenerisalsoanobject.
Listenerwaitsuntilitreceivesanevent.Oncetheeventisreceived,the
listenerprocesstheeventanthenreturns.

Source & Listener
Itsconceptisquitesimple:asourcegeneratesaneventandsendsittooneor
morelisteners.Inthisscheme,thelistenersimplywaitsuntilitreceivesanevent.
Onceaneventisreceived,thelistenerprocessestheeventandthenreturns.
Thisprovidesanimportantbenefit:notificationsaresentonlytolistenersthat
wanttoreceivethem.
ToperformEventHandling,weneedtoregisterthesourcewiththelistener.

Registering Listener
Asourcemustregisterlistenersinorderforthelistenerstoreceive
notificationsaboutaspecifictypeofevent.Eachtypeofeventhasitsown
registrationmethod.Hereisthegeneralform:
publicvoidaddTypeListener(TypeListenerel)
Typeisthenameoftheevent,andelisareferencetotheeventlistener.
Forexample,themethodthatregistersakeyboardeventlisteneriscalled
addKeyListener().Themethodthatregistersamousemotionlisteneriscalled
addMouseMotionListener().

DelegationEventModel

Event Classes in Java

Event Classes in Java

Event Classes in Java

Event Listener Interfaces
Asexplained,thedelegationeventmodelhastwoparts:sourcesand
listeners.
Listenersarecreatedbyimplementingoneormoreoftheinterfaces
definedbythejava.awt.eventpackage.
Whenaneventoccurs,theeventsourceinvokestheappropriatemethod
definedbythelistenerandprovidesaneventobjectasitsargument

Event Listener Interfaces
Listener Interface Methods
ActionListener •actionPerformed()
AdjustmentListener•adjustmentValueChanged()
ComponentListener
•componentResized()
•componentShown()
•componentMoved()
•componentHidden()
ContainerListener
•componentAdded()
•componentRemoved()
FocusListener
•focusGained()
•focusLost()

Event Listener Interfaces
ItemListener •itemStateChanged()
KeyListener
•keyTyped()
•keyPressed()
•keyReleased()
MouseListener
•mousePressed()
•mouseClicked()
•mouseEntered()
•mouseExited()
•mouseReleased()
MouseMotionListener
•mouseMoved()
•mouseDragged()
MouseWheelListener •mouseWheelMoved()

Java MouseListenerInterface
TheJavaMouseListenerisnotifiedwheneveryouchangethestateof
mouse.ItisnotifiedagainstMouseEvent.
TheMouseListenerinterfaceisfoundinjava.awt.eventpackage.It
hasfivemethods

Methods of MouseListenerinterface
The signature of 5 methods found in MouseListenerinterface are given
below:
publicabstractvoidmouseClicked(MouseEvente);
publicabstractvoidmouseEntered(MouseEvente);
publicabstractvoidmouseExited(MouseEvente);
publicabstractvoidmousePressed(MouseEvente);
publicabstractvoidmouseReleased(MouseEvente);

MouseListenerSample Program

Java KeyListenerInterface
TheJava KeyListeneris notified whenever you change the state
of key.
It is notified against KeyEvent. The KeyListenerinterface is found
in java.awt.eventpackage, and it has three methods

Interface declaration
Following is the declaration
forjava.awt.event.KeyListenerinterface:
publicinterfaceKeyListenerextendsEventListener
Sr. no. Method name Description
1. public abstract void
keyPressed(KeyEvente);
Itisinvokedwhenakeyhas
beenpressed.
2. public abstract void
keyReleased(KeyEvente);
Itisinvokedwhenakeyhas
beenreleased.
3. publicabstractvoidkeyTyped
(KeyEvente);
Itisinvokedwhenakeyhas
beentyped

THANK YOU