Java Event Handling
Java Event Handling
11.0 INTRODUCTION
Java applets are used to create
graphical and user-interactive applications. The applets
designed so far were graphical in nature but did very little when it comes to demonstrating
their user-interactive capability. User interaction means a
specific action is generated
and an appropriate result is displayed to the user when he interacts with an
application
say by clicking mouse or enteringa character using the keyboard. In Java, all the activities
that occur between the user and the application are termed as events. Events play a
significant role in applet programming as they facilitate to include user-interactivity in
applets.
In this unit, you will get a
comprehensive overview of event handling in Java
programming.
11.2 EVENTS
InJava, an event is an object which specifies the change of state in the source. It is
generated whenever an action takes place like a mouse button is clicked or text is
modified. Java's AWT (Abstract Window Toolkit) is
these actions between the program and the user. Java
responsible for communicating
packages such as [Link],
Self-Insructional
AMaterial 143
ort event handling
support event
Event Handling [Link]
and
[Link], [Link]. event
mechanism. event and all.
Thegenerated
is generated. ofevent, etc.
a n event are sent to the
When an action takes place, type
nformation about it such as timeofits n. This
occurrence,
This code
code determines
NOTES within the
program.
response cani be sent to
appropriate event handling code provided appropriate
how the allocated event will behandled sothat an
the user.
Frame
Frame
Unhandled Event
Panel Propagated
be handled by the
major drawback in this approach is that events could
The
container of that component. Another problem is
component that generated it or by the
that events are frequently sent to those components that cannot process them, thus
wasting a lot of CPU cycles.
generated
For example, as shown in the Figure 11.2, when the mouse is clicked on Button,
an event is generated. If the Button has a registered listener to handle the event, this
event is sent to Button, processed and the output is returned to the user. However, ifit NOTES
has no registered listener the event will not be propagated upwards to Panel or Frame.
Frame
Panel
Event Handler
Button
Event Generated
Self-Instructional
Material 145
Classes
Event
Event Hundling Table 11.1
Description
uscr selectsa menv
when a
gencratcd
that is
Class the event a list item.
double-clicks
represents
ActionEvent I button or
ilem, presses
when a
change has occ
occuTed i
inteon
NOTES cvent
that is
generated
class
defines an
It represents
an
tEvent that signals thatsome
AdjustmentEvent the
The
Adjustmen
ADJUSTMENT_VALUE_CHANGED
serollbar.
constant,
when a menu ite
changehas occurred. which is generated
clicked or a list it 1
the cvcnt
item is
m is
represcnts checkbox
ItemEvent lt desclectod.
selected o r interacts
the user
when
sclected. event that is
gencratcd
represents
the
l interacts
KeyEvent application through
keys.
generaled
when the u s e r
with|
the event that is
the
It represents
mouse. enter
MouseEvent through the when the user ters or
the applicationthc event
generated
that is
lt
textareas,
represents
TextEvent of the wind.
text fields or when the
state
changes textin cvent that is generated
the
lt represents
notifies
This class like but it only
| ChangeEvent of an object
on the change
generated
object. the text component
change ofthesource
event which
is generated by
the or when
some text is
It represents its position
CaretEvent when the caret (crusor) changes
selected.
Sources
Table 11.2 Event
Description
Event Souree pressed.
events when the button is
generates action
Button window changes.
events when the
state of the
generates window
Window entered in text field or
text area.
events when a text is
generates text
Text components -
Self-Instrucunul
146 Matriul
where,
e is the reference to an Event Handling
object of
The AdjustmentListener Interface ActionEvent class
When an adjustment event
occurs, the
adjustmentValueChanged
defined by the AdjustmentListener
interface is invoked.
() method NOTES
The general form ofthe
void adjustmentValueChanged () method
adjustmentValueChanged is
where, (AdjustmentEvent e)
e is the
reference to an object
of
The ItemListener Interface AdjustmentEvent class
The method
invoked when the
itemStateChanged
state of
() defined by the
ItemListener intertace
an item is changed. The is
passed as method parameter. object of ItemEvent class is
'Self-Instructionul
Material 147
Event Handling
The TextListener Interface
The method
invoked when
unvoked textValueChanged () defincd by the
t
when the text inside tthe text arca or text Gcid
ficld
TextListener
oid
(D
Event Handling is
The output of the HTML code
the
replaced
Delcgation Event
ln this example,
KeyListener
interface. In the
i n i t ()
the applet itself is the
andimplements be noted that here
model. listener for key events. It should the methods defined by
ESelf as a
events. This applet
implements all
(b) The Delegation Sourceand listener for these
Event model use
the KeyListener interface.
event listeners to
the event handling mechanism using
demonstrate
handle events. to
Example 11.2: An applet
(c) Java 1.0 model is MouseListener interface
more efficient than
Delegation Event
import [Link]. *;
model.
import [Link].*;
(d) The Delegation
Event model uses import [Link]. *;
handleEvent ()
method to process
events. /*
width=400 height=400>
What is an even, code="MouseEventHandle"
applet
event source and
event listener? </applet>
interface defines
methods to handle
events related to
adaMouselistener (this);
keys in Java.
Self-Instructional
150 Material
1 saving the
coordinates of mouse
Kcoordinate=0; Event Handling
Ycoordinate=20 ;
message= "Mouse
clicked event";
repaint ();
NOTES
/ Mouse entered
event
public void handling
mouseEntered (MouseEvent me)
1 saving the
coordinates of mouse
Xcoordinate=0;
Ycoordinate=20;
message= "Mouse entered
event";
repaint ()
Xcoordinate=0 ;
Ycoordinate=20;
message= "Mouse pressed event";
repaint ();
Self-Instructional
Material 151
Event Handling
repaint ():
NOTES
I displaying essage in the applet window at the
/location of x, y coordinates.
current
public void paint (Graphics g
HTML code:
<HTML
HEAD><TITLE>MouseEventHandling«/TITLE> </HEAD>
<BODY>
<CENTER>
APPLET coDE="[Link]" WIDTH=800
HEIGHT=700>
</APPLET>
</CENTER
</BODY>
</HTML>
The output of the HTML code is
Self-Instructional
152 Material
Event Handling
/*
<applet code="AdapterExample" width=300 height=300>
</applet>
AdapterExample adex;
public MouseMotion (AdapterExample adex)
[Link]=adex;
Self-Instructional
Material 153
Event Handling
move
event
/handle mouse
(MouseEvent me
mouseMoved
public void
moved") i
NOTES [Link] ( "Mouse
<HTML> ADAPTER
MOTION
WITH
ADS<TITLE>MOUSE
</HEAD>
<BODY>
WIDTH=800
<CENTER> A d a p t e r E x a m p l e . c l a s s "
coDE="
CAPPLET
HEIGHT=700>
</APPLET>
</CENTER>
</BODY
</HTML
is
The output of the HTML code
M Computer
Mause noved
extends Applet class. In the
In this example, the AdapterExample class
listener for mouse motion events. It
init() method, the applet registers itself as
a
Self-Instructional
154 Material
11.5 SUMMARY Event Handling
tn this unit, you have learned that:
In Java, all the
activities that occur
termed as events. between the user and the
An event is an
application are NOTES
object which specifies the
generated, whenever an change
action takes place like of state in the source. It is
is modified. a mouse button is clicked or text
Java packages such as
[Link], java. awt and
support event handling mechanism.
The
[Link]
working of an event-driven
handling model. program is governed by its underlying
event
.The Java 1.0 Event model
of containment. of Java for event processing was based on the
concept
.The Delegation Event
Model defines a logical
based on the
concept source and listener. Aapproach
of to handle events. It
is
sends it to one or more listeners source generates an event and
that can handle the
Java provides various classes event
and interfaces to handle the generated events.
.Some of the event classes are
ItemEvent, KeyEvent, ActionEvent, AdjustmentEvent,
etc. WindowEvent, ChangeEvent, CaretEventt
Some of the event listener
interfaces
ActionListener, are
AdjustmentListener, ItemListener, KeyListener,
WindowListener, ChangeListener, CaretListener, etc.
11.6 KEY TERMS
.HTML elements:It indicates to the
HTML document and marks the Web browser that the page loaded is an
beginning and end of the HTML document.
.HEAD element: It specifies the header
infomation about the HTML document
such as title, keywords, and so on.
.Color class: It provides various methods to
use any color wanted to
defining various color constants, which can be directly used by display by
color of choice. specifying the
Self-Instructional
Material 155
Event Handling
4. Truc
5. KeyListener an
cvent ener interfac
listen
inmplements
which
are the classes,
6. Adapter classes are
with blank body. we can define a n cfinc new
NOTES and define all ts method
the code because
7. arehelpful
in reducing and implemcnting only
th those
Adapter classes an adapted class events.
vent listener class by extending all the
implementing
vents that are required
instead of
EXERCISES
11.8 QUESTIONS AND
-
Short-Answer Questions
1. What is an event?
in Java.
for event handling
2. List the models proposed
five cvent
classes.
3. What are event classes? List
Long-Answer Questions
model.
1. Discuss the delegation event event listener.
event source and
2 Explain the meaning ofevent, event handling in Jau
interfaces that support a.
3. List and describe the
classes and
Self-Instructional
156 Material