You are on page 1of 12

1

Mailam Engineering College


(Approved AICTE, New Delhi Affiliated to Anna University Chennai
& Accredited by National of Accreditation (NBA New Delhi
Mailam Villupuram (Dt), Pin 604 304
DEPARTMENT ! CMP"TER APP#$CAT$N%
&e' Programming
"N$T $V
"N$T $V PAC(A)E%

A!T pac"a#e $ %ayo&ts $ Containers $ Event 'ac"a#e $ Event (odel $ 'aintin# $ )arba#e
Collection * (&ltithreadin# $ %an#&a#e 'ac"a#es+
* Mar+, -ue,tion, .it/ An,.er,
01 &/at i, t/e 2la,, an3 inter4a2e in 5a6a to 2reate t/rea3 an3 ./i2/ i, t/e mo,t
a36antageou, met/o37
Thread class and ,&nnable interface can be &sed to create threads and &sin#
,&nnable interface is the -ost advanta#eo&s -ethod to create threads beca&se we need
not e.tend thread class here+
*1 &/en 8ou .ill ,8n2/roni9e a pie2e o4 8our 2o3e7
!hen yo& e.pect yo&r code will be accessed by different threads and these threads
-ay chan#e a partic&lar data ca&sin# data corr&ption+
31 &/at i, 3aemon t/rea3 an3 ./i2/ met/o3 i, u,e3 to 2reate t/e 3aemon t/rea37
Dae-on thread is a low priority thread which r&ns inter-ittently in the bac" #ro&nd
doin# the #arba#e collection operation for the /ava r&nti-e syste-+ setDae-on -ethod is
&sed to create a dae-on thread+
41 Are t/ere an8 glo'al 6aria'le, in :a6a, ./i2/ 2an 'e a22e,,e3 '8 ot/er part o4
8our program7 Apr/May 2011
No, it is not the -ain -ethod in which yo& define variables+ )lobal variables are not
possible beca&se concept of encaps&lation is eli-inated here+
;1 &/at i, t/e 3i44eren2e 'et.een appli2ation, an3 applet,7 Nov 2011
Application -&st be r&n on local -achine whereas applet needs no e.plicit
installation on local -achine+
Application -&st be r&n e.plicitly within a /ava*co-patible virt&al -achine whereas
applet loads and r&ns itself a&to-atically in a /ava*enabled browser+
Application starts e.ec&tion with its -ain -ethod whereas applet starts e.ec&tion
with its init -ethod+
Application can r&n with or witho&t #raphical &ser interface whereas applet -&st r&n
within a #raphical &ser interface+
61 <o. 3oe, applet re2ogni9e t/e /eig/t an3 .i3t/7
Usin# #et'ara-eters( -ethod+
=1 &/en 3o 8ou u,e 2o3e'a,e in applet7
!hen the applet class file is not in the sa-e directory, codebase is &sed+
>1 <o. 3o 8ou ,et ,e2urit8 in applet,7
Prepared By
Ms. A.Subathra Devi., AP/MCA
2
&sin# set0ec&rity(ana#er( -ethod
?1 &/at i, an e6ent an3 ./at are t/e mo3el, a6aila'le 4or e6ent /an3ling7
NOV 2010
An event is an event ob/ect that describes a state of chan#e in a so&rce+ In other
words, event occ&rs when an action is #enerated, li"e pressin# b&tton, clic"in# -o&se,
selectin# a list, etc+ There are two types of -odels for handlin# events and they are1 event*
inheritance -odel & event*dele#ation -odel+
001 &/at i, ,our2e an3 li,tener 7
so&rce 1 A so&rce is an ob/ect that #enerates an event+ This occ&rs when the internal
state of that ob/ect chan#es in so-e way+
listener 1 A listener is an ob/ect that is notified when an event occ&rs+ It has two
-a/or re2&ire-ents+ 3irst, it -&st have been re#istered with one or -ore so&rces to
receive notifications abo&t specific types of events+ 0econd, it -&st i-ple-ent
-ethods to receive and process these notifications+
001 &/at i, a3apter 2la,,7
An adapter class provides an e-pty i-ple-entation of all -ethods in an event
listener interface+ Adapter classes are &sef&l when yo& want to receive and process only
so-e of the events that are handled by a partic&lar event listener interface+ 4o& can define
a new class to act listener by e.tendin# one of the adapter classes and i-ple-entin# only
those events in which yo& are interested+
0*1 &/at i, meant '8 2ontrol, an3 ./at are 3i44erent t8pe, o4 2ontrol, in A&T7
Controls are co-ponents that allow a &ser to interact with yo&r application and the
A!T s&pports the followin# types of controls1
%abels, '&sh B&ttons, Chec" Bo.es, Choice %ists, %ists, 0crollbars, Te.t Co-ponents+
031 &/at i, t/e 3i44eren2e 'et.een 2/oi2e an3 li,t7
A Choice is displayed in a co-pact for- that re2&ires yo& to p&ll it down to see the
list of available choices and only one ite- -ay be selected fro- a choice+
A %ist -ay be displayed in s&ch a way that several list ite-s are visible and it
s&pports the selection of one or -ore list ite-s+
041 &/at i, a la8out manager an3 ./at are 3i44erent t8pe, o4 la8out manager,
a6aila'le in 5a6a1a.t7
A layo&t -ana#er is an ob/ect that is &sed to or#ani5e co-ponents in a
container+ The different layo&ts are available are 3low%ayo&t, Border%ayo&t,
Card%ayo&t, )rid%ayo&t and )ridBa#%ayo&t+
0;1 &/i2/ 2ontainer, u,e a @or3er la8out a, t/eir 3e4ault la8out7
!indow, 3ra-e and Dialo# classes &se a Border%ayo&t as their layo&t+
061 &/i2/ 2ontainer, u,e a !lo. la8out a, t/eir 3e4ault la8out7
'anel and Applet classes &se the 3low%ayo&t as their defa&lt layo&t+
0=1 &/at are .rapper 2la,,e,7
!rapper classes are classes that allow pri-itive types to be accessed as ob/ects+
0>1 &/at i, t/e 3i44eren2e 'et.een ,et an3 li,t7
0et stores ele-ents in an &nordered way b&t does not contain d&plicate ele-ents,
whereas list stores ele-ents in an ordered way b&t -ay contain d&plicate ele-ents+
Prepared By
Ms. A.Subathra Devi., AP/MCA
3
0?1 De4ine T/rea37
The basic &nit of pro#ra- e.ec&tion+ A process can have several threads r&nnin#
conc&rrently, each perfor-in# a different /ob, s&ch as waitin# for events or perfor-in# a
ti-e*cons&-in# /ob that the pro#ra- doesn6t need to co-plete before #oin# on+ !hen a
thread has finished its /ob, the thread is s&spended or destroyed+
*01 <o. T/rea3, are 2reate3 in :a6a7
Threads are created in two ways+ They are by e.tendin# the Thread class and by
i-ple-entin# ,&nnable interface+ 778+ !hat are the vario&s states of a thread+ The
followin# fi#&re shows the states that a thread can be in d&rin# its life and ill&strates which
-ethod calls ca&se a transition to another state
*01 &/at i, multit/rea3e3 programming7
(&ltithreaded pro#ra- contains 9 or -ore parts that can r&n conc&rrently+ Each
part of s&ch a pro#ra- is called a thread, and each thread defines a separate path of
e.ec&tion+
**1 &/at i, t/rea3 priorit87
:ava assi#ns to each thread a priority that deter-ines how that thread sho&ld be treated
with respect to the others+ Thread priorities are inte#ers that specify the
relative priority of one thread to another+
*31 De4ine %8n2/roni9ation7
In the :ava pro#ra--in# lan#&a#e, when 0ynchroni5ed is applied to a -ethod or
code bloc", #&arantees that at -ost one thread at a ti-e e.ec&tes that code+ This process
is "nown as 0ynchroni5ation+
*41 &/at 3o 8ou mean '8 T/rea3 %2/e3uling7
E.ec&tion of -&ltiple threads on a sin#le C'U in so-e order is called
sched&lin#+ The :ava r&nti-e environ-ent s&pports a very si-ple, deter-inistic sched&lin#
al#orith- called fi.ed*priority sched&lin#+ This al#orith- sched&les threads on the basis of
their priority relative to other ,&nnable threads+
*;1 De4ine Dea3 #o2+7
A special type of error that relates specifically to -&ltitas"in# is dead loc",
which occ&rs when two threads have a circ&lar dependency on a pair of synchroni5ed
ob/ects+
*61 &/at i, T/rea3 Pool7
A thread pool is a -ana#ed collection of threads that are available to perfor- tas"s+
Thread pools &s&ally provide1
I-proved perfor-ance when e.ec&tin# lar#e n&-bers of tas"s d&e to red&ced per*
Prepared By
Ms. A.Subathra Devi., AP/MCA
4
tas" invocation overhead+
A -eans of bo&ndin# the reso&rces, incl&din# threads, cons&-ed when e.ec&tin# a
collection of tas"s+
*=1 &/at i, an EA2eption7
An exception is an event that occ&rs d&rin# the e.ec&tion of a pro#ra- that disr&pts
the nor-al flow of instr&ctions d&rin# the e.ec&tion of a pro#ra-+
*>1 <o. an EA2eption i, /an3le3 in 5a6a7
A pro#ra- can catch e.ceptions by &sin# a co-bination of the try, catch, and finally
state-ents+ The try state-ent identifies a bloc" of code in which an e.ception can occ&r+
The catch state-ent identifies a bloc" of code, "nown as an e.ception handler, that can
handle a partic&lar type of e.ception+
*?1 &/at i, t/e u,e o4 !inall8 'lo2+7
The finally state-ent identifies a bloc" of code that cleans &p re#ardless of whether an
e.ception occ&rred within the try bloc"+ A try state-ent -&st be acco-panied by at least
one catch state-ent or a finally state-ent and -ay have -&ltiple catch state-ents+
301 Di,tingui,/ 'et.een T/ro. an3 T/ro.,7
Throw :ava pro#ra--in# lan#&a#e "eyword that allows the &ser to throw an
e.ception or any class that i-ple-ents the ;throwable; interface+ Throws "eyword can be
&sed in -ethod declarations that specify which e.ceptions are not handled within the
-ethod b&t rather passed to the ne.t hi#her level of the pro#ra-+
301 <o. Rea3er an3 &riter Cla,,e, are u,e3 in 5a6a7
,eader and !riter are the abstract s&perclasses for character strea-s in /ava+io+ ,eader
provides the A'I and partial i-ple-entation for readers**strea-s that read 78*bit
characters**and !riter provides the
A'I and partial i-ple-entation for writers**strea-s that write 78*bit chaacters+
3*1 &/at t/e u,e o4 $nput%tream an3 utput%tream 2la,,e,7
To read and write <*bit bytes, pro#ra-s sho&ld &se the byte
strea-s, descendants of Inp&t0tra- and=&tp&t0trea-+ Inp&t0trea- and =&tp&t0trea-
provide the A'I and partial i-ple-entation for input streams (strea-s that read <*bit bytes
and output strea-s (strea-s that write <*bit bytes+
331 &/at i, t/e u,e o4 Data input an3 ouput ,tream,7
Data inp&t and o&p&t 0trea-s are &sed to read or write pri-itive data types in a
-achine*independent for-at+
341 &/at i, t/e u,e o4 @u44ere3 %tream,7
B&ffered strea-s, b&ffer data while readin# or writin#, thereby red&cin# the
n&-ber of accesses re2&ired on the ori#inal data so&rce+ B&ffered strea-s are typically
-ore efficient than si-ilar nonb&ffered strea-s and are often &sed with other strea-s+
3;1 &/at i, 4iltere3 %tream7
The /ava+io pac"a#e provides a set of abstract classes that define and partially
i-ple-ent filter streams+ A filter strea- filters data as it6s bein# read fro- or written to the
strea-+
361 &/at i, t/e u,e o4 %eBuen2e$nput%tream7
0e2&enceInp&t0trea- can be &sed to create a sin#le inp&t strea- fro-
-&ltiple inp&t so&rces+
3=1 &/at i, <ot :a6a 7
Prepared By
Ms. A.Subathra Devi., AP/MCA
5
An easily c&sto-i5able !eb browser developed by 0&n (icrosyste-s, which is
written in the :ava(T( pro#ra--in# lan#&a#e+
3>1 &/at i, an Applet7
Applet is a :ava application, which can be e.ec&ted in :>(, enabled web browsers+
3?1 &/at are met/o3, a6aila'le in t/e Applet 2la,,7
init * To initialize the applet each ti-e it6s loaded (or reloaded+
start * To start the applet6s e.ec&tion, s&ch as when the applet6s loaded or
when the &ser revisits a pa#e that contains the applet+
stop * To stop the applet6s e.ec&tion, s&ch as when the &ser leaves the
applet6s pa#e or 2&its the browser+
destroy * To perfor- a final cleanup in preparation for &nloadin#+ 7??+
Distin#&ish between paint and &pdate -ethod@
paint is basic display -ethod+ (any applets i-ple-ent the paint -ethod to
draw the applet6s representation within a browser pa#e+
Update is a -ethod that can &se alon# with paint to i-prove drawin#
perfor-ance+
401 &/at i, A&T7
A collection of #raphical &ser interface ()UI co-ponents that were i-ple-ented
&sin# native*platfor- versions of the co-ponents+ These co-ponents provide that s&bset of
f&nctionality which is co--on to all native platfor-s+ %ar#ely s&pplanted by the 'ro/ect
0win# co-ponent set+
401 #i,t out ,ome "$ 2omponent, a6aila'le in A&T7
B&ttons (/ava+awt+B&tton
Chec"bo.es (/ava+awt+Chec"bo.
0in#le*line te.t fields (/ava+awt+Te.t3ield
%ar#er te.t display and editin# areas (/ava+awt+Te.tArea
%abels (/ava+awt+%abel
%ists (/ava+awt+%ist
'op*&p lists of choices (/ava+awt+Choice
0liders and scrollbars (/ava+awt+0crollbar
Drawin# areas (/ava+awt+Canvas
(en&s (/ava+awt+(en&, /ava+awt+(en&Ite-, /ava+awt+Chec"bo.(en&Ite-
Containers (/ava+awt+'anel, /ava+awt+!indow and its s&bclasses
4*1 &rite ,ome met/o3,, ./i2/ are u,e3 to a33 "$ 2omponent, in Applet7
add * Adds the specified Co-ponent+
re-ove * ,e-oves the specified Co-ponent+
set%ayo&t * 0ets the layo&t -ana#er+
431 &rite t/e <tml 2o3e to loa3 an Applet in t/e 'ro.,er7
AA''%ET C=DEBAppletSuclass+class !IDTCBan!nt CEI)CTBan!ntD
AEA''%ETD
441 <o. parameter, are pa,,e3 to an Applet7
The para-eters are passed to an Applet by &sin# para- ta#
AA''%ET C=DEBAppletSuclass+class !IDTCBan!nt CEI)CTBan!ntD
A'A,A( NA(EBparameter1Name >A%UEBaValueD
A'A,A( NA(EBparameter2Name >A%UEBanotherValueD AEA''%ETD
Prepared By
Ms. A.Subathra Devi., AP/MCA
6
4;1 &rite t/e attri'ute, o4 t/e Applet tag7
A A''%ET
FC=DEBA0E B codease"#$G C=DE B
applet%ile
FA%T B alternate&extG
FNA(E B applet!nstanceNameG !IDTC B
pixels
CEI)CT B pixels
FA%I)N B ali'nmentG F>0'ACE B
pixelsG FC0'ACE B pixelsG
D
FA 'A,A( NA(E B applet(arameter1 >A%UE B value
DG
FA 'A,A( NA(E B applet(arameter2 >A%UE B value
D
+ + +
Falternate)&M$G AEA''%ETD
FA 'A,A( NA(E B applet(arameter2 >A%UE B value
DG
+ + +
Falternate)&M$G AEA''%ETD
461 &/at i, t/e u,e o4 transient +e8.or37
A "eyword in the :ava pro#ra--in# lan#&a#e that indicates that a field is not part of
the seriali5ed for- of an ob/ect+ !hen an ob/ect is seriali5ed, the val&es of its transient
fields are not incl&ded in the serial representation, while the val&es of its non*transient
fields are incl&ded+
4=1 &rite ,/ort note on i,Ali6e() an3 5oin()7
isAlive( and /oin( -ethods are &sed to deter-ine whether a thread has finished or
not+ 3irst, yo& can call isAlive( on the thread+ This -ethod is defined by Thread, and
its #eneral for- is1
final *oolean isAlive+,
The isAlive( -ethod ret&rns tr&e if the thread &pon which it is called is still r&nnin#+
It ret&rns false otherwise+
!hile isAlive( is occasionally &sef&l, the -ethod that yo& will -ore co--only &se to
wait for a thread to finish is called /oin(+ The #eneral for- is1
final void -oin+, thro.s !nterrupted/xception
This -ethod waits &ntil the thread on which it is called ter-inates+
4>1 Create a ; C; gri3 an3 4ill, it .it/ *4 'utton, ea2/ la'ele3 .it/ it, in3eA in
5a6a1 Nov 2010
)rid%ayo&t'l&s #lp B new )rid%ayo&t'l&s(H, ?, 7H, 7HI
#lp+setCol!ei#ht(7, 9I
#lp+setCol!ei#ht(9, 7I
#lp+set,ow!ei#ht(9, 7I
f+set%ayo&t(#lpI
for (int r B HI r A JI rKK L
for (int c B HI c A JI cKK L
f+add(new :B&tton(rK;,;KcI
M
M
Prepared By
Ms. A.Subathra Devi., AP/MCA
7
06 Mar+ -ue,tion, .it/ An,.er,
10 EAplain a'out A&T Pa2+age in 'eri4l8D Nov 2010
A!T stands for Abstract !indow ToolNit+
It is a portable )UI library between 0olaris and !indows OPENT and (ac 0yste-
Q+R(soon for stand*alone applications andEor applets+
It provides -any classes for pro#ra--ers to &se+ It is yo&r connection between yo&r
application and the native )UI+ The A!T hides yo& fro- the &nderlyin# details of the
)UI yo&r application will be r&nnin# on and th&s is at very hi#h level of abstraction+
Co-ponents are added to and then laid o&t by layo&t -ana#ers in Containers+
There are two &ser*interface classes in the A!T to foc&s on1 Co-ponents and
Containers+
Container,D
Containers (3ra-es, Dialo#s, !indows and 'anels can contain co-ponents
and are the-selves co-ponents, th&s can be added to Containers+
Containers &s&ally handle events that occ&rred to the Co-ponents, altho&#h
nothin# prevents yo& fro- handlin# events in the co-ponent+
The -ethod of handlin# events in the Container (i+e+ 3ra-e is preferred over
the latter, since we want to centrali5e event handlin#+
All Containers have co--on f&nctionality, d&e to the fact they are derived
fro- Container which incl&des -any pre*defined event handlin# -ethods+
These events are &sef&l for handlin# &ser inp&t in a speciali5ed (i+e+ s&b*class
or derived Container class where yo&6d override the defa&lt behavior s&ch as
the appearance (i+e+ font, color, etc+ by overridin# the -ethods+
0o-e co--on -ethods are1
add(Co-ponent
add(0trin#, Co-ponent
re-ove(Co-ponent
#etCo-ponents(
set%ayo&t(%ayo&t(ana#er
#et%ayo&t(
Component,D
They are #enerally the st&ff that the &ser interacts with+
Co-ponents are B&ttons, Te.tAreas, 0crollbars, etc+ in other words the visible
UI controls that the &ser interacts with, all of which have been added to a
Container+
0o-e co--on -ethods are1
#etBac"#ro&nd(
setBac"#ro&nd(Color
#et3ont(
set3ont(3ont
-o&seDown(Event, int, int
show(
resi5e(int, int
paint()raphics
&pdate()raphics
Prepared By
Ms. A.Subathra Devi., AP/MCA
8
-ove(int, int
*1 #a8out ManagerD Nov 2011
Cow Co-ponents are ;laid o&t; within a Container is described by the
%ayo&t(ana#er class+
0ince the %ayo&t(ana#er class is abstract, we can not &se it directly+ !e -&st s&b*
class it and provide o&r own f&nctionality or &se a derived class of %ayo&t(ana#er
(i+e+ Border%ayo&t, Card%ayo&t, )rid%ayo&t, etc already created for &s+
There are -an different layo&t sche-es, b&t the ones pre*defined for &s are
Border%ayo&t This sche-e lays o&t the Co-ponent in P ways
North * Northern part of the Container
0o&th * 0o&thern part of the Container
East * Eastern part of the Container
!est * !estern part of the Container
Center * Centered in the Container
Card%ayo&t * Allows for what !indows pro#ra--ers have called for years
;tabbed dialo#s; or dyna-ic dialo#s(now available on all versions of
Netscape+
)rid%ayo&t * Allows for the layo&t of Co-ponents in a #rid *li"e fashion rather
than ;North; or ;Center;+
)ridBa#%ayo&t * CT(%Table*ish style of layo&t
3low%ayo&t * Allows for Co-ponent to be laid o&t in a row(or flow and
ali#ned(left, ri#ht, center+
None * No layo&t, the Container will not atte-pt to reposition the
Co-ponents d&rin# a &pdate+
To &se a layo&t we -&st call set%ayo&t( for the Container with an instance of a
%ayo&t(ana#er+
Types of %ayo&t available are1
3low %ayo&t1 It is the defa&lt layo&t -ana#er for -ost co-ponents+ It lays
o&t co-ponents hori5ontally,
Border%ayo&t1 Arran#es &p to five co-ponents in five positions1 Center, East,
!est, North, and 0o&th+
)rid%ayo&t1 'lace co-ponents in a ,ow vs Col&-n -atri.+ Co-ponents fill
slots startin# on top row, left to ri#ht, then -ove to ne.t row down+
)ridBa#%ayo&t1 The -ost powerf&l layo&t -ana#er is the )ridBa#%ayo&t
shown below+ It is very &sef&l when yo& have an elaborate interface with lots
of co-ponents+ )ridBa#%ayo&t places a co-ponent accordin# to the settin#s
in an instance of the helper class )ridBa#Constraints+
31 E6ent Pa2+ageD
The /ava+awt+event pac"a#e defines classes and interfaces &sed for event handlin# in
the A!T and 0win#+ The -e-bers of this pac"a#e fall into three cate#ories1
Events
The classes with na-es endin# in ;Event; represent specific types of
events, #enerated by the A!T or by one of the A!T or 0win#
co-ponents+
%isteners
The interfaces in this pac"a#e are all event listenersI their na-es end with
;%istener;+ These interfaces define the -ethods that -&st be i-ple-ented
Prepared By
Ms. A.Subathra Devi., AP/MCA
9
by any ob/ect that wants to be notified when a partic&lar event occ&rs+
Note that there is a %istener interface for each Event class+
Adapters
Each of the classes with a na-e endin# in ;Adapter; provides a no*op
i-ple-entation for an event listener interface that defines -ore than one
-ethod+ !hen yo& are interested in only a sin#le -ethod of an event
listener interface, it is easier to s&bclass an Adapter class than to
i-ple-ent all of the -ethods of the correspondin# %istener interface+
41 A2tion E6entD
An ob/ect of this class represents a hi#h*level action event #enerated by an A!T
co-ponent+
Instead of representin# a direct &ser event, s&ch as a -o&se or "eyboard event,
ActionEvent represents so-e sort of action perfor-ed by the &ser on an A!T
co-ponent+
The 'et!1+, -ethod ret&rns the type of action that has occ&rred+
The #etAction2ommand+, -ethod ret&rns a 0trin# that serves as a "ind of na-e
for the action that the event represents+
'etModifiers+, ret&rns a val&e that indicates the "eyboard -odifiers that were in
effect when the action event was tri##ered+
The #eneral type is
p&blic class ActionEvent e.tends A!TEvent L
EE '&blic Constr&ctors
p&blic ActionEvent (=b/ect so&rce, int id, 0trin# co--andI
p&blic ActionEvent (=b/ect so&rce, int id, 0trin# co--and, int -odifiersI
EE '&blic Constants
p&blic static final int ACTI=NS3I,0T I B7HH7
p&blic static final int ACTI=NS%A0T I B7HH7
p&blic static final int ACTI=NS'E,3=,(ED I B7HH7
p&blic static final int A%TS(A0N I B<
p&blic static final int CT,%S(A0N I B9
p&blic static final int (ETAS(A0N I BJ
p&blic static final int 0CI3TS(A0N IB7
EE '&blic Instance (ethods
p&blic 0trin# #etActionCo--and (I
p&blic int #et(odifiers (I
EE '&blic (ethods =verridin# A!TEvent
p&blic 0trin# para-0trin# (I
M
Action%istener1
This interface defines the -ethod that an ob/ect -&st i-ple-ent to listen for action
events on A!T co-ponents+
!hen an ActionEvent occ&rs, an A!T co-ponent notifies its re#istered
Action%istener ob/ects by invo"in# their action'erfor-ed( -ethods+
The #eneral type is
p&blic abstract interface Action%istener e.tends /ava+&til+Event%istener L
EE '&blic Instance (ethods
p&blic abstract void action'erfor-ed (ActionEvent eI
M
;1 PaintingD Nov 2010
Prepared By
Ms. A.Subathra Devi., AP/MCA
10
To &nderstand how A!T6s paintin# A'I wor"s, helps to "now what tri##ers a paint
operation in a windowin# environ-ent+
In A!T, there are two "inds of paintin# operations1 syste-*tri##ered paintin#, and
application*tri##ered paintin#+
0yste-*tri##ered 'aintin#
In a syste-*tri##ered paintin# operation, the syste- re2&ests a
co-ponent to render its contents, &s&ally for one of the followin#
reasons1
The co-ponent is first -ade visible on the screen+
The co-ponent is resi5ed+
The co-ponent has da-a#e that needs to be repaired+ (3or e.a-ple,
so-ethin# that previo&sly obsc&red the co-ponent has -oved, and a
previo&sly obsc&red portion of the co-ponent has beco-e e.posed+
App*tri##ered 'aintin#
In an application*tri##ered paintin# operation, the co-ponent decides it
needs to &pdate its contents beca&se its internal state has chan#ed+ (3or
e.a-ple,+ a b&tton detects that a -o&se b&tton has been pressed and
deter-ines that it needs to paint a ;depressed; b&tton vis&al+
The 'aint (ethod1
,e#ardless of how a paint re2&est is tri##ered, the A!T &ses a ;callbac"; -echanis-
for paintin#, and this -echanis- is the sa-e for both heavywei#ht and li#htwei#ht
co-ponents+
This -eans that a pro#ra- sho&ld place the co-ponent6s renderin# code inside a
partic&lar overridden -ethod, and the tool"it will invo"e this -ethod when it6s ti-e
to paint+
The -ethod to be overridden is in /ava+awt+Co-ponent1
p&blic void paint()raphics #
!hen A!T invo"es this -ethod, the )raphics ob/ect para-eter is pre*confi#&red
with the appropriate state for drawin# on this partic&lar co-ponent1
61 MultiET/rea3ingD Nov 2010
A thread e.ec&tes a series of instr&ctions+ Every line of code that is e.ec&ted is done
so by a thread+ 0o-e threads can r&n for the entire life of the applet, while others
are alive for only a few -illiseconds+
The class /ava+lan#+ Thread is &sed to create and control threads+
+ To create a thread, a new instance of this class -&st be created+ Cowever, the
thread does not start r&nnin# ri#ht away+ Thread+start( -&st be called to act&ally
-a"e the thread r&n+
There are two ways to create a thread1
E.tend the Thread class+ !ith this techni2&e the new class inherits fro- the
class Thread+ The thread can start r&nnin# in the class6s r&n -ethod+
I-ple-ent the ,&nnable interface+ This techni2&e is probably -ore co--on
than e.tendin# the Thread class+ It is not necessary to define a new class to r&n
the thread+ If a thread is to start r&nnin# in the applet, it -&st &se the ,&nnable
interface+ The applet cannot inherit fro- both the Thread and Applet classes+ An
applet with the ,&nnable interface -&st have a r&n( -ethod for the thread to
start+
The Thread class has seven constr&ctors+ All of the- create a new thread+
The constr&ctors are the followin#1
Prepared By
Ms. A.Subathra Devi., AP/MCA
11
Thread(
Thread(,&nnable
Thread(Thread)ro&p
Thread(0trin#
Thread(Thread)ro&p,0trin#
Thread(,&nnable,0trin#
Thread(Thread)ro&p,,&nnable,0trin#
The constr&ctors can &se three possible para-eters1
0trin# The na-e of the new thread is the para-eter 0trin#+ A thread can #et
its na-e by callin# Thread+#etNa-e(+
Thread)ro&p The new thread will belon# to the #ro&p specified by the
para-eter Thread)ro&p+ A Thread)ro&p can be &sed to or#ani5e a thread+
,&nnable The ,&nnable para-eter is an ob/ect that has i-ple-ented the
,&nnable interface+ The thread will start e.ec&tin# in the r&n( -ethod of the
,&nnable para-eter when Thread+start( has been called+
There are -any -ethods in the Thread class+ 0o-e of the-, s&ch as destroy(, don6t
see- to have been i-ple-ented yet, and -ay never be+
start( This -ethod starts the thread+ It starts e.ec&tin# in the r&n( -ethod
of its ,&nnable tar#et that was set when the constr&ctor was called+ This
-ethod can be called only once+
s&spend( This -ethod s&spends the e.ec&tion of the thread+ It re-ains
s&spended &ntil res&-e( is called+
res&-e( This -ethod res&-es the e.ec&tion of a s&spended thread+ It has
no effect on a thread that is not s&spended+
stop( This -ethod stops and "ills a r&nnin# thread+ C&rrently, the thread
does not stop &nless it is r&nnin#+ If it is s&spended, it does not die &ntil it
starts r&nnin# a#ain+ Cowever, this -ay be fi.ed so-eday+
sleep(int -Esleep(int -,int n The thread sleeps for - -illiseconds, pl&s n
nanoseconds+
E.a-ple1
p&blic class InfiniteThreadE.a-ple e.tends Applet i-ple-ents ,&nnable
L
Thread -yThreadI
p&blic void init(
L
0yste-+o&t+println(;in init( ** startin# thread+;I
-yThreadB new Thread(thisI
-yThread+start(I
M
p&blic void start(
L
0yste-+o&t+println(;in start( ** res&-in# thread+;I
-yThread+res&-e(I
M
p&blic void stop(
L
0yste-+o&t+println(;in stop( ** s&spendin# thread+;I
-yThread+s&spend(I
M
p&blic void destroy(
L
0yste-+o&t+println(;in destroy( ** stopin# thread+;I
Prepared By
Ms. A.Subathra Devi., AP/MCA
12
-yThread+res&-e(I
-yThread+stop(I
M
p&blic void r&n(
L
int iBHI
for(II
L
iKKI
0yste-+o&t+println(;At ; K i K ; and co&ntin#T;I
try
L
-yThread+sleep(7HHHI
M
catch (Interr&ptedE.ception e LM
M
M
M
Prepared By
Ms. A.Subathra Devi., AP/MCA

You might also like