You are on page 1of 54

J@

ConLrols
W ConLrols are subclasses of Component
W ConLrols allow a user Lo lnLeracL wlLh an
appllcaLlon ln varlous ways
ConLrols supporLed by J@
abels
ush buLLons
Check boxes
Cholce llsLs
lsLs
Scroll bars
@exL edlLlng
LxcepL labels whlch are passlve conLrols all conLrols
generaLe evenLs when Lhey are accessed by Lhe user
ddlng ConLrols
W @o lnclude a conLrol ln a wlndow
1 CreaLe an lnsLance of conLrol
2 dd Lo wlndow by calllng add( ) (deflned by Conta|ner)
W ComponenL add(ComponenL compCb[)
compCb[ ls an lnsLance of Lhe conLrol Lo be added
reference Lo compCb[ ls reLurned
3 lmplemenLs lnLerface speclflc Lo conLrol
4 8eglsLer an evenL llsLener for each conLrol Lo monlLor lL
W Cnce llsLener ls lnsLalled evenLs are auLomaLlcally senL
Lo lL
W now Lhey'll be dlsplayed along wlLh parenL wlndow
8emovlng ConLrols
W @o remove a conLrol when noL needed
vold remove(ComponenL ob[) (ueflned by
ConLalner)
W ob[ ls a reference Lo Lhe conLrol Lo be removed
W ll conLrols can be removed by removell( )
abels
W Cb[ecL of abel
W ulsplays a sLrlng
W uon'L lnLeracL wlLh user
W ueflnes Lhe followlng consLrucLors
abel( ) creaLes a blank label
abel(SLrlng sLr) creaLes a label conLalnlng sLrlng sLr
SLrlng ls lefL[usLlfled
abel(SLrlng sLr lnL how)
W ConLalnlng Lhe sLrlng sLr uslng Lhe allgnmenL speclfled by how
how musL be Lhe consLanLs abelLl@ abel8lCP@ or abelCLn@L8
Some funcLlons
W vold seL@exL(SLrlng stt)
SeL or change Lhe LexL ln a label
W SLrlng geL@exL( )
CbLaln Lhe currenL label
W vold seLllgnmenL(lnL bow)
seL allgnmenL of Lhe sLrlng wlLhln Lhe label
W lnL geLllgnmenL( )
CbLaln Lhe currenL allgnmenL
Lxample
// uemonsLraLe abels
lmporL [avaawL*
lmporL [avaappleL*
/*
appleL codeabeluemo wldLh300 helghL200
/appleL
*/
publlc class abeluemo exLends ppleL
publlc vold lnlL()
abel one new abel(Cne)
add(one)

Lxample
uLLons
W Cb[ecLs of uLLon
W uLLon deflnes 2 consLrucLors
uLLon( ) creaLes buLLon wlLhouL label
uLLon(SLrlng stt)
W @o seL label
vold seLabel(SLrlng stt)
W @o 8eLrleve label
SLrlng geLabel( )
LvenL handllng for uLLon
W Cn presslng acLlon evenL generaLed
W SenL Lo reglsLered llsLeners Lo recelve acLlon
evenL noLlflcaLlons from buLLon
W Lach llsLener lmplemenLs nt|onL|stener
lnLerface deflnlng acLlonerformed( )
W cLlonLvenL ob[ecL ls supplled as argumenL Lo
meLhod Lo ldenLlfy buLLon elLher Lhrough
8eference Lo buLLon generaLlng evenL
8eference Lo sLrlng le label of buLLon
Lxample
W Pere abel or sLrlng ls used Lo deLermlne
pressed buLLon
W abel ls obLalned by geLcLlonCommand( ) on
cLlonLvenL ob[ecL passed Lo
acLlonerformed( )
noLher example
W @o deLermlne pressed buLLon
CbLaln ob[ecL from geLSource( )
Jhlch meLhod Lo use?
W lf durlng execuLlon
we change Lhe label durlng execuLlon
Cr
uslng buLLons havlng same label
W Lasler Lo deLermlne pressed buLLon by lLs
ob[ecL reference
Check oxes
W Pave label wlLh each box descrlblng opLlons
W used lndlvldually or parL of group
W Cb[ecLs of Checkbox class
W Checkbox supporLs Lhese consLrucLors
Checkbox( )
Checkbox(SLrlng stt)
Checkbox(SLrlng stt booleoo oo)
Checkbox(SLrlng stt booleoo oo cbeckboxCtoop cbCtoop)
Check oxes(conLd)
W @o reLrleve currenL sLaLe
boolean geLSLaLe( )
W @o seL lLs sLaLe
vold seLSLaLe(boolean oo)
W CbLaln Lhe currenL label
SLrlng geLabel( )
W @o seL Lhe label
vold seLabel(SLrlng stt)
Check oxes(conLd)
W Cn selecLlng or deselecLlng an lLem evenL ls
generaLed
W SenL Lo reglsLered llsLeners Lo recelve
noLlflcaLlons from LhaL componenL
W lsLeners lmplemenL temL|stener deflnlng
lLemSLaLeChanged( ) wlLh lLemLvenL ob[ecL as
argumenL
Cb[ecL conLalns lnformaLlon abouL Lhe evenL
Lxample
Checkbox Croup or 8adlo uLLons
W @o creaLe a seL of muLually excluslve check boxes
1 ueflne Lhe group
2 use Lhls group when checkbox ls consLrucLed
W Cb[ecLs of Lype CheckboxCroup
CheckboxCroup() consLrucLor Lo creaLe an empLy
group
W ueLermlne Check box currenLly selecLed
Checkbox geLSelecLedCheckbox( )
W SeL a check box
seLSelecLedCheckbox( )
Lxample
Cholce ConLrols
W uefaulL consLrucLor Lo creaLe an empLy llsL
@o add a selecLlon Lo Lhe llsL call add( )
W lLems are added ln order ln whlch calls Lo add( ) are done
W ueLermlnlng currenLly selecLed lLem
SLrlng geLSelecLedlLem( ) or lnL geLSelecLedlndex( )
W @o obLaln no of lLems ln llsL
lnL geLlLemCounL( )
W SeLLlng Lhe currenLly selecLed lLem uslng
vold selecL(lnL lndex)
vold selecL(SLrlng name)
W CbLalnlng Lhe name assoclaLed wlLh lLem aL an lndex
SLrlng geLlLem(lnL lndex)
Pandllng Cholce lsLs
W Lach Llme a cholce ls selecLed an lLem evenL ls
generaLed
W SenL Lo any reglsLered llsLeners Lo recelve lLem
evenL from LhaL componenL
W Lach llsLener lmplemenLs temL|stener
lnLerface deflnlng |temStateChanged( ) wlLh
temLvent ob[ecL ls supplled as an argumenL
Lxample
lsLs
W lsL( ) or lsL(lnL num8ows) or lsL(lnL num8ows boolean mulLlpleSelecL)
1 llows selecLlon of only one lLem aL a Llme
2 num8ows speclfles no of enLrles ln llsL LhaL wlll always be vlslble (oLhers can be
scrolled)
3 lf mulLlpleSelecLLrue user may selecL 2 or more lLems aL a Llme lf lL ls false only
one lLem
W 2 ways Lo add
vold add(SLrlng name) or vold add(SLrlng name lnL lndex)
W lsLs LhaL allow only slngle selecLlon
@o deLermlne currenLly selecLed lLem
W SLrlng geLSelecLedlLem( ) or lnL geLSelecLedlndex( )
8eLurns a sLrlng conLalnlng name of lLem lf more Lhan one lLem ls selecLed or lf no
selecLlon null ls reLurned
8eLurns lndex of Lhe lLem llrsL lLem ls aL lndex 0 lf more Lhan one lLem ls selecLed or lf
no selecLlon has yeL been made 1 ls reLurned
W lsLs LhaL allow mulLlple selecLlon
@o deLermlne currenL selecLlons
W SLrlng geLSelecLedlLems( ) or lnL geLSelecLedlndexes( )
lsLs(conLd)
W CbLalnlng no of lLems ln llsL
lnL geLlLemCounL()
W SeLLlng Lhe currenLly selecLed lLem
vold selecL(lnL lndex)
W CbLaln name assoclaLed wlLh an lLem lf lndex
ls glven
SLrlng geLlLem(lnL lndex)
Pandllng lsLs
W @o process llsL evenLs lmplemenL nt|onL|stener
lnLerface
W Lach Llme a lsL lLem ls doub|en||ned an cLlonLvenL
ob[ecL ls generaLed
geLcLlonCommand( ) used Lo reLrleve name of newly
selecLed lLem
W Lach Llme an lLem ls selecLed or deselecLed wlLh a
s|ng|e n||n an lLemLvenL ob[ecL ls generaLed
geLSLaLeChange( ) used Lo deLermlne wheLher a selecLlon
or deselecLlon Lrlggered Lhls evenL
W geLlLemSelecLable( ) reLurns a reference Lo ob[ecL LhaL
Lrlggered Lhls evenL
Lxample
Scroll ars
W CurrenL value of scroll bar ls relaLlve Lo mln and max values lndlcaLed by
sllder box(or Lhumb)
W Je can cllck Lo move Lhe currenL value of scroll bar by one unlL ln
dlrecLlon of Lhe arrow(aL boLh sldes)
W user can cllck Lo [ump ln a dlrecLlon by some lncremenL larger Lhan 1
called as page up or page down
Scroll ars(conLd)
W Cb[ecLs of Scrollbar class
W ConsLrucLors
Scrollbar( ) CreaLes vert|na| snro|| bar
Scrollbar(lnL sLyle)
Scrollbar(lnL sLyle lnL lnlLlalvalue lnL LhumbSlze lnL mln lnL max)
2
nd
and 3
rd
forms speclfy orlenLaLlon of scroll bar
W lf sLyle ls Snro||barVLk1CL a vert|na| snro|| bar ls creaLed
W lf sLyle ls Snro||barnCkCN1L the snro|| bar |s hor|zonta|
3
rd
lnlLlal value of scroll bar ls passed ln |n|t|a|Va|ue
thumbs|ze no of unlLs represenLed by helghL of Lhe Lhumb
m|n and max Mlnlmum and maxlmum values for Lhe scroll bar
Scroll ar(conLd)
W @o obLaln currenL value of scroll bar
lnL geLvalue( )
W @o seL Lhe currenL value
vold seLvalue(lnL oewvoloe)
W Sllder box lnslde Lhe scroll bar wlll be poslLloned Lo reflecL Lhe new value
W @o reLrleve Lhe mlnlmum and maxlmum values
lnL geLMlnlmum( )
lnL geLMaxlmum( )
W y defaulL 1 ls added or subLracLed Lo scroll bar each Llme lL ls scrolled up or down
one llne
@o change Lhls lncremenL
W vold seLunlLlncremenL(lnL oewloct)
W y defaulL pageup and pagedown lncremenLs are 10
@o change Lhls value
W vold seLlocklncremenL(lnL oewloct)
Pandllng Scroll ars
W lmplemenL Lhe d[ustmentL|stener |nterfane
W Lach Llme user lnLeracLs wlLh bar d[ustmentLvent
ob[ecL ls generaLed
W getd[ustment1ype( ) Lell Lype of ad[usLmenL
@he Lypes of ad[usLmenL evenLs are as follows
CCk_uLC8LMLn@ agedown evenL generaLed
CCk_lnC8LMLn@ ageup evenL generaLed
@8Ck bsoluLe Lracklng evenL generaLed
unl@_uLC8LMLn@ lnedown buLLon ln scroll bar pressed
unl@_lnC8LMLn@ lneup buLLon ln scroll bar pressed
Lxample
@exLlleld or LdlL ConLrol
W Slnglellne LexLenLry area
W llow enLerlng and edlLlng of Lhe sLrlngs uslng arrow keys cuL
pasLe keys and mouse selecLlons
W Subclass of @exLComponenL
W ConsLrucLors
@exLlleld( )
@exLlleld(lnL numChars)
@exLlleld(SLrlng sLr)
@exLlleld(SLrlng sLr lnL numChars)
1
sL
creaLes a defaulL LexL fleld
2
nd
creaLes a LexL fleld LhaL ls numChars wlde
3
rd
lnlLlallzes LexL fleld wlLh Lhe sLrlng sLr
4
Lh
lnlLlallzes a LexL fleld and seLs lLs wldLh
@exLlleld(conLd)
W @o reLrleve sLrlng conLalned ln LexL fleld or @o seL Lhe LexL
SLrlng geL@exL( ) or vold seL@exL(SLrlng sLr)
W @o selecL a LexL porLlon and Lhen reLrleve lL
vold selecL(lnL sLarLlndex lnL endlndex) CharacLers beglnnlng aL sLarLlndex and endlng aL endlndex1
SLrlng geLSelecLed@exL( )
W @o conLrol wheLher Lhe conLenLs of a LexL fleld may be modlfled by Lhe user
boolean lsLdlLable( )
W @o deLermlne edlLablllLy
vold seLLdlLable(boolean canLdlL)
W lf we wanL Lo enLer LexL LhaL ls noL dlsplayed llke password @hen Lo dlsable echolng of
characLers
vold seLLchoChar(char ch) Speclfles a slngle characLer LhaL @exLlleld wlll dlsplay when characLers are
enLered (acLual characLers Lyped wlll noL be shown)
W @o check a LexL fleld Lo see lf lL ls ln Lhls mode
boolean echoCharlsSeL( ) meLhod
W @o reLrleve Lhe echo characLer
char geLLchoChar( )
Lxample
@exL rea
W @o enLer daLa lnLo mulLlple llnes
@exLrea( )
@exLrea(lnL oomlloes lot oomcbots)
@exLrea(SLrlng stt)
@exLrea(SLrlng stt lot oomlloes lot oomcbots)
@exLrea(SLrlng stt lot oomlloes lot oomcbots lot s8ots)
lo 5
tb
we coo speclfy Lhe scroll bars s8ots most be ooe of tbese
voloes
SC8C8S_C@P
SC8C8S_nCnL
SC8C8S_PC8lZCn@_Cn
SC8C8S_vL8@lC_Cn
W Subclass of @exLComponenL
@exL rea
W SupporLs(meLhods deflned ln @exLComponenL)
geL@exL( )
seL@exL( )
geLSelecLed@exL( )
selecL( )
lsLdlLable( )
seLLdlLable( )
W @exLrea adds Lhe followlng meLhods
vold append(SLrlng stt)
vold lnserL(SLrlng stt lot loJex)
vold replace8ange(SLrlng stt lot stottloJex lot eoJloJex)
LvenLs relaLed Lo @exL rea
W @exL areas only generaLe goLfocus and losL
focus evenLs
Lxample
ayouL Managers
W uLomaLlcally poslLlons conLrols wlLhln wlndows
W Jhenever a conLalner ls reslzed (or slzed for 1
sL
Llme)
lL ls used Lo poslLlon each componenL
W ulsplay componenLs uslng 8 @opoLLom
organlzaLlon
W keeps Lrack of a llsL of componenLs by Lhelr names and
noLlfled each Llme we add a new componenL
W !ava has several predeflned LayoutManager classes
W Je can also poslLlon conLrols manually
Pow Lo seL new?
W Lach Conta|ner ob[ecL has a layouL manager assoclaLed
wlLh lL
lnsLance of a class lmplemenLlng LayoutManager
|nterfane
W vold seLayouL(ayouLManager loyootObj)
@o dlsable defaulL layouL managerpass nu||
lf noL called defaulL layouL manager ls used
W @o deLermlne shape and poslLlon of each componenL
manually
set8ounds( ) deflned by Component
MeLhods
W Jhenever conLalner needs Lo be reslzed layouL
manager ls enqulred by
mlnlmumayouLSlze( )
preferredayouLSlze( )
W Lach componenL LhaL ls managed by a layouL
manager conLalns
geLreferredSlze( )
geLMlnlmumSlze( )
W Je may overrlde Lhese meLhods for conLrols
CLherwlse defaulL values are used
llowayouL
W uefaulL layouL manager
W lmplemenLs a slmple layouL sLyle slmllar Lo words ln a LexL
edlLor
W ComponenLs are lald ouL from Lhe upperlefL corner 8
@
W ConsLrucLors
llowayouL( )
llowayouL(lnL how)
llowayouL(lnL how lnL horz lnL verL)
W 1
sL
creaLes defaulL layouL whlch nenters nomponents and |eaves S
p|xe|s spane beLween each componenL
W 2
nd
speclfy how each llne ls allgned values for how
llowayouLLl@ or llowayouLCLn@L8 or llowayouL8lCP@
W 3
rd
speclfles horlzonLal and verLlcal space
Lxample
orderayouL
W lmplemenLs a common layouL for Loplevel wlndows
W 4 narrow flxedwldLh componenLs aL Lhe edges and one large area ln Lhe cenLer
4 sldes referred as norLh souLh easL wesL
Mlddle area called as cenLer
W ConsLrucLors
orderayouL( )
orderayouL(lnL horz lnL verL)
W 1
sL
creaLes a defaulL border layouL
W 2
nd
speclfles Lhe horlzonLal and verLlcal space beLween componenLs
W lollowlng consLanLs speclfy Lhe reglons
orderayouLCLn@L8 or orderayouLSCu@P or orderayouLLS@
or orderayouLJLS@ or orderayouLnC8@P
W use Lhese consLanLs wlLh add( ) as
vold add(ComponenL compObj Object teqloo)
Pere compObj ls tbe compooeot to be oJJeJ ooJ teqloo
speclfles wbete tbe compooeot wlll be added
Lxample
lnseLs
W lf we wanL Lo leave space b/w conLalner LhaL
holds componenLs and wlndow
lnseLs geLlnseLs( ) deflned by Conta|ner
W 8eLurns lnseLs ob[ecL conLalnlng Lop boLLom lefL and
rlghL whlch are used by layouL manager Lo lnseL Lhe
componenLs when lL lays ouL wlndow
W ConsLrucLor
lnseLs(lnL Lop lnL lefL lnL boLLom lnL rlghL)
W Lop lefL boLLom and rlghL speclfy amounL of space
beLween Lhe conLalner and lLs encloslng wlndow
Lxample
CrldayouL
W ays ouL componenLs ln 2u grld
W Je can deflne no of rows columns ln consLrucLors
CrldayouL( )
CrldayouL(lnL num8ows lnL numColumns )
CrldayouL(lnL num8ows lnL numColumns lnL horz lnL
verL)
W 1
sL
creaLes a slnglecolumn grld
W 2
nd
speclfles no of rows and columns
lf num8ows 0 means unllmlLedlengLh columns
lf numColumns0 means unllmlLedlengLh rows
W 3
rd
speclfles horlzonLal and verLlcal space beLween componenLs
Lxample
CardayouL
W SLores several dlfferenL layouLs
W Lach can be LreaLed as a separaLe lndex card ln a deck
LhaL can be shuffled so LhaL any card ls on Lop aL a
glven Llme
W useful for user lnLerfaces wlLh opLlonal componenLs
LhaL can be dynamlcally enabled and dlsabled upon
user lnpuL
W ConsLrucLors
CardayouL( )
CardayouL(lnL horz lnL verL)
1
sL
creaLes a defaulL card layouL
2
nd
speclfles horlzonLal and verLlcal space beLween componenLs
CardayouL(conLd)
W Pow Lo make card for Lhe deck
Cards are Lhe ob[ecLs of 9ane|
anel musL have CardLayout se|ented as lLs layouL manager
@o add cards Lo a panel
W vold add(ComponenL panelCb[ Cb[ecL name)
name name of card panels added Lo a panel
dd Lhls panel Lo maln appleL panel
W now provlde a way Lo selecL any card
lnclude a buLLon for each card ln deck
W fLer creaLlon of deck program acLlvaLes a card by calllng one of
Lhe followlng meLhods
vold flrsL(ConLalner deck) or vold lasL(ConLalner deck)
or vold nexL(ConLalner deck) or vold prevlous(ConLalner
deck) or vold show(ConLalner deck SLrlng cardname)
W deck 8eference Lo conLalner (usually a panel) LhaL holds Lhe cards
W cardname name of a card
Lxample

You might also like