You are on page 1of 17

Java Programming

Comprehensive Concepts and Techniques


THIRD EDITION
Shell Cashman Star!s "ic
SHE##$
C%SH"%N
!Program Development & '((
I#a)d
i IScro*Panc
+I,-RE ./0
Program
Development
The program development
ccle I1or the Classics on
D2D program consists o3
las!s that correspond to the
si4 development ccle
phases5 as sho6n in Ta)le
./07
r
DIVIIOPMIHT PHAU TASK(S) I
i %nal8e the requirements %nal9e the Classics on D2D pro)lem7
: Design the solution
Design the user inter3ace7 Design the logic to solve the pro)lem7
( 2alidate the design
Con3irm 6ith the user that the design solves the pro)lem in a satis3actor
manner7
' Implement the design
Translate the design into code7 Include internal documentation ;comments and
remar!s< 6ithin the code that e4plains the purpose o3 the code statements7
= Test the solution
Test the program7 +ind and correct an errors ;de)ug< until it is error/3ree7
> Document the solution Print copies o3 the application code7
%nalsis and
Design
+igure ./: on the
ne4t page sho6s the
requirements
document that
initiates the
development ccle 3or
the Classics on D2D
program7 The
? o@i6c'AnC"<
;to B
ltA2rt87vdo3O8
Sol 3r 3l!
=T-DO
irm"m P%O
68
tf c3 C D I+rEtnc
D"enu*dr
F JComt4G*o4
$E%P
0B:
t Bt
IB
tt6
3ormatted
Ite4tPane
Table 7-1 Classies on DVD Progra De!elo"ent Tas#s
' & Chapt er . & S6i ng I nt er 3 aces 6i t h Sor t i ng and Sear chi ng
requirements
document speci3ies
the reason 3or the
request and lists the
required components7
l7rirrAA; %ll Hi3t%lA
m@ IA @Hml nmv% && %3IacB7 itA
AI nI @ & &
H & r r/cAo7ti@Am
DJrA
Jt6
rllAJ' n7l J ;1
&
l7ltiri- h67 KiJt in mrA1LFl ort8tA %6A /AA
rtAiJHlA t2 mm3l InmnE I HimnD JtM3 rvh NGrrrrFr
iv3trK it 7tn unr il rocnFIAA HNtArr i
t
REO-
EST
+OR
NEB
%PP#I
C%TI
ON
Date su)mittedP Novem)er (7 :CC.
Su)mitted )vP C lassies on l<M lG C ollcctors H lu)
PurposeP
Our users have requested an eas7to/use des!top inter3ace to organi9e their classic D2D
collections7 The program should 6or! on a variet o3 plat3orms7 %t this time 6e 6ould li!e a
prototpe7
%pplication titleP Clonics on D2D
%lgorithm1P Please mate a user/3riendl inter3ace 6ith the 3ollo6ingP
DisplaP
Drop/do6n list 6ith Title5 Studio5 and $ear choices +ormatted output area
"enu
+ile menu 6 ith E4it command
Edit menu 6ith Inserl and Search commands
Search command should displaE su)menu listings 3or Title5
Studio5 and $ear
NotesP
Bc have seen some programs 6ith Java1s Decorated #oo! and +eel5 ll Ins a !ind o3 multimedia
Lloo!L lo it7 Can ou use that in the prototpeQ
%lso5 man o3 our users li!e Ihe shortcut !es on menus7
The Inscit menu command sltou!l immediatel sort the list a3ter adding tlic i66 D2D7 lo
al6as present the D2Ds in older7
Some o3 our mem)ers have man classic movies on D2D7 so the displa area s)ou!l scroll to
displa as man titles as nccessai7
The displa area should use all caps 3or column headings and )old 3or the movie names7
%pproval
A
%pproval statusP R %pproved
ReEected
%pproved )vP Ru) Brin!le
DateP Novem)er '7 :CC.
%stlgned loP J7 Stai!s7 Programmer
+I,-RE 7$
P
R
O
*
#
E
"

%
N
%
#
$
S
I
S

%
n

I
n
t
e
r
n
e
t
/
)
a
s
e
d

c
o
l
l
c
c
t
o
r
s
g
r
o
u
p

h
a
s
r
e
q
u
e
s
t
e
d

a

p
r
o
t
o
t

p
e

p
r
o
g
r
a
m

l
o

o
r
g
a
n
i
8
7
e

t
h
e
i
r
c
l
a
s
s
i
c

m
o
v
i
e
s
7
T
h
e

p
r
o
)
l
e
m

t
h
a
t
t
h
e

p
r
o
g
r
a
m

s
h
o
u
l
d

s
o
l
v
e

i
s
t
h
e

o
r
g
a
n
i
9
a
t
i
o
n

o
3
c
l
a
s
s
i
c

m
o
v
i
e
s
o
n

I
<
2
0
G

)


t
i
t
l
e
5
c
o
r
r
e
s
p
o
n
d
i
n
g

s
t
u
d
i
o
5
a
n
d

e
a
r
r
e
l
e
a
s
e
d
7
T
h
e

p
r
o
g
r
a
m

s
h
o
u
l
d

a
l
l
o
6

t
h
e

u
s
e
r
t
o

i
n
s
e
r
t
n
e
6

t
i
t
l
e
s
5
s
e
a
r
c
h
5
s
o
r
t
5
a
n
d

d
i
s
p
l
a

7
t %ll HiDAn
-KE &@H IA in
Ali I3 7@ DSi IB
el!AA &&&T1H&
hltmr3 hJA %vrvsl @Gt orl8rA l!6M rn inArAF)
tM1mmJI InmnE I serinU nEh %ntftr&nf J"nmiI
it Pn1 "nr i3 JJAv@Fp6n nAU
!
t@ itProgram
Development & '(=% scarch is a gcncric term
lor the process a computer per3orms to
ascertain the presence o3 a speci3ic piece
o3 data in a list or storage location7 The
data entered ) the user is called the
scarch argument7 % user enters a scarch
argument7 and the computer loo!s it up7
+or e4ample5 a user o3 the Classics on
D2D program ma !no6 the title o3 a
movie )ut not !no6 i3 the movie is
located in the collection7 In another
e4ample5 users ma need to 3ind more
in3ormation a)out the movie5 such as the
studio or ear it 6as released7 In a search5
the user enters the title o3 the movie5 and
the application program compares the title
6ith the titles in the programs list and
then displas the results7
% sort is a generic term that re3ers to
the 6as to organi8e data alphanumer/
icall )ased on speci3ic criteria7
Tpicall5 users request a sort )ased on a
certain 3ield o3 data5 such as sort ) title5
sort ) studio5 or sort ) ear7 Once the
sort 3ield is determined5 the application
program reorders or alpha)eti9es all
accompaning data )ased on that speci3ic
3ield7
DESI,N THE SO#-TION Once ou
have anal9ed the pro)lem5 the ne4t step
is to design the user inter3ace7 The
requirements document 3or the Classics on
D2D program requests a S6ing inter3ace
6ith a scrolla)le te4t area to displa a
3ormatted5 detailed listing o3 the movies
V+igure ./(a<7 % drop/do6n list compo/
nent is required to choose ho6 to sort7
*ecause the request is speci3ic a)out
desired menu options5 a menu stor)oard
;+igure ./() on the ne4t page< is a good
6a to veri3 6ith the customer that the
menu titles and shortcut !es sat is3 their
needs7 % shortcut !e7 or access !e7 is a
special !e com)ination used to invo!e
commands on a menu7 % shortcut !e
usuall is an alternative to using the
mouse7 +or e4ample5 6hen ou use cm
WHP to cop selected te4t5 ou are using a
shortcut !e7 In +igure ./()7 the
underlined letters in the menu sstem arc
shortcut !es
7
Cla37sicX on D2D
+ile Edi
t
Sort )
P
3IC-R3 7-ia
r/3rJlto Nil %ll
@@ h D@t IB @&&&@&& li3NA
6/6 t-Hd D"@ AH@H(
hlimr3 rr'(r) hJA %vrvsl @Gt nrt8rA ;!64 rrJ BlN
tM1mmJI lrJrAnE 3rrGEJEH I rarini nEN 3c6
8LA
? OC
Tit-
I
ST-DIO 2E%R TIT#E
&et
r*ttr*t)
6S6ing Components and Java +oundation Classes & '(Y
S6ing Components and Java +oundation Classes
Recall that S6ing is a light6eight set o3 components that arc a part o3 the lava +oundation
Classes ;++C<7 The J+C is de3ined as containing the 3eatures Summari9ed in Ta)le ./:7
Table 7-$ +eat,res of t-e la!a +o,n.ation Classes (/+C)
1 l,r!,
DUCMPTIO0 1
S6ing Components
S6ing components arc implemented 6ilh no native ccdc7 Native re3ers to code and components that are em)edded in the operating
sstem7 S6ing components5 there3ore5 ma not appear identical to the components users are used to seeing in their operating sstem7 +or
e4ample5 a Bindo6s message )o4 loo!s di33erent 3rom a lOptionPane message )o4 )ecause the Bindo6s version uses the native
common diatog 6indo67 S6ing components include everthing 3rom panels to )uttons to split panes to ta)les7 S6ing components )egin
6ith an uppercase D7 $ou have used lOptionPane S6ing components in previous chapters7
Plugga)le #oo! and +eol Support The loo! and 3eel methods o33er an program using S6ing a choice o3 ho6 6indo6s5 title )ars5 and other components 6ill displa7
+or e4ample5 the same program can use either the Java loo! and 3eel or the Bindo6s loo! and 3eel7
%ccessi)ilit %PI
This D+C %PI ena)les assistive technologies5 such as screen readers and *raille displas5 to o)tain in3ormation 3rom the user
interlace7
JavE :D %PI
The :D %PI ena)les programmers to incorporate high/qualit :D graphics5 te4t5 and images in applications and applets7
Drag and Drop Support
+irst populari9ed ) "icroso3t Bindo6 applications5 Dava contains D+C methods to provide drag and drop capa)ilities )et6een a
Java application and a native application7
0 0
S6ing Components versus %BT Components
%lthough the Java : Plat3orm still supports the %BT components5 it is recommended that
vou use S6ing components instead )ecause the contain more methods to manipulate and
implement )asic ,-I/related 3eatures7 S6ing components arc not rcstrictcd to the least common
denominator5 or the 3eatures present on even/ plat3ormU there3ore5 the have more 3unctionalit
than %BT components7 Even the simplest S6ing components have capa)ilities )eond 6hat the
%BT components o33er7
The maEor di33erence )et6een %BT components and S6ing components is that the latter arc
implemented 6ith no native code7 There3ore5 S6ing components are )ac!6ard compati)le5 6hich
means the can )e added on to older versions o3 the IDI7 S6ing components arc considered
light6eight5 6hich means the )orro6 screen resources 3rom the operating sstem and there3ore
do not have to support their o6n7 Heav6eight components5 such as those used 6ith the %BT5
are associated 6ith their o6n native resource5 or peer7 #ight6eight components ma!e more
e33icient use ol their resources5 provide consistenc across plat3orms5 and have a cleaner loo!
and 3eel integration7 Recall that %BT components arc in the Eava7a6t pac!age5 6hile S6ing
components arc in the Eava47su ing pac!age7
r71lFniHlI'llHA3FDGii3l7riiniD %ll DiDhli RFv@l &@A IA@@@nl 6otv% %3lvi3tetJ7in 6"r &J 6 D6@ IBD@AH iGJ@ 37FWv rD-' @&AT&@ Ji
3 h4 ivrvsl AiJi inJ rrrirrt liv4 rg' GKIm InnnE I nE r) 1(r nEN it Ptv 1irr il rFen71tJ3@ 3F.i@v il
O1ttrA ii 5m1 tmr i3 7rr
it
0' & Chapt er . & S6i ng I nt er 3 aces 6i t h Sor t i ng and Sear chi ng
S6i ng Component s
%lthough the Dava : plat3orm still supports the %BT components5 C(l it is
recommended that ou use S6ing components instead )ecause the contain more methods to
manipulate and implement )asic ,-I/related 3eatures7
Ta)le ./( lists sonic o3 the advantages o3 using S6ing components7
0 ADVA0TACIS 21 USI0C S3I0C COMPOHI0IS 0
07 S6ing *uttons and la)els can displa images instead o35 or in addition to5 te4t7
:7 *orders ma )e dra6n around most S6ing components7
(7 S6ing components do not have to )e rectangular7 D*uttons5 lor e4ample5 can )e round7
'7
%ssistive technologies can o)tain in3ormation Irom S6ing components7 +or e4ample5 a screen reader easil can read the te4t that is displaed on a D*utton or ;#a)el7
=7
S6ing components allo6 programmers and developers to speci3 the loo! and 3eel o3 the ,-I7 * contrast5 %BT components al6as have the loo! and 3eel o3 the
native plat3orm7
>7
Some S6ing components use e4ternal o)Eects to hold in3ormation a)out the state o3 the component7 The S6ing slider )ar5 DS3cder5 3or e4ample5 automaticall is
associated 6ith a *oundedRange"odel o)Eect to hold its current value and range o3 legal values7
"ost o3 the %BT components have a counterpart in the S6ing pac!agc7 The %BT )utton
class5 3or e4ample5 is named *utton5 6hile the S6ing )utton class is named E*utton7 The %BT
container7 +rame5 has a ;+rame version that provides the Same 3unctionalit )ut 6ith more
3eatures7 Ta)le ./0 displas a comparison list o3 %BT and S6ing components7
0 S3I0C COUPO0I0IS
A3T COMPOHIH1S ADVA0TACIS Of Ult04 SiVT04 COMPO0t05S 0
;#a)el #a)el "a use te4t or image or )oth
;Te4t+ield Te4t+ield
"ore methods to manipulate te4tU can )e overlaid 6ith a vie6port lor additional laering o3 components
DChcc!*o4 ;Option*utton
Chec!)o4
-ses t6o di33erent controls 6ith more methods5 6hereas %BT groups a single control
< *utton *utton "a have images on the )uttonU )utton ma ta!e di33erent shapes
;Com)o*o4 Com)c*o4 "a ta!e t6o di33erent 3ormsP edita)le and unedita)le
;Tc4t%rca Tc4t%rca
Separates the scrolling capa)ilit5 si9e5 6ord 6rap5 and other properties to a vie6port or EScrollPanc 3or
more 3le4i)ilit
D Panel Panel Supports all the S6ing methods inherited 3rom containers
< +rame +rame -ses a contcnt pane container to provide more 3le4i)ilit
;%pplet %pplet Supports the <+C8S6ing component architecture
I"enu*ar "cnu*ar
Creates menus as )uttons 3or greater 3le4i)ilit in creating shortcut menus and pop/up menus
r71Dm"Jll %ll HiDAh -J5 7p vliFr tt HpJ@ IB J& r3ori"v r33taA nH h6tv rl-' @JJTHA
3tlirr iJ rnt3TA hGF HBGMD hn trG nri8rt %6 AlG <Kl"r2@@8nJ1 lnmnZ r@ )6r nEN rmvnr G"nAiI r6L@ il Pn nrr it it
Table 7-i S&ing Co"onent A.!antages
Table 7-7 S&ing Co"onents !ers,s A3T Co"onents
S6ing Container Hierarch & ''0
S6ing Container Hierarch
The container hierarch 3or an 6indo6 or applet that includes S6ing components must have a S6ing top/level container rather than an %BT
container7 % top/level container5 also called a root container is an o)Eect at the top o3 the
container hierarch that displas a )order and title )ar7 +or e4ample5 a main 6indo6 should
)e implemented as a J+ramc instance rather than as a +rame instance i3 the main 6indo6
6ill contain S6ing components Ta)le ./= displas the container hierarch7 Containers e4ist
in )oth %BT and S6ing pac!ages5 )ut the pla a more important role in S6ing
applications7
Table 7-S T-e S&ing Container Hierar*-'
rrn or *ontainir
"!,rost
S3I0C I8AUPtt 1
Top/#evel Containers
The components at the top o3 an S6ing containment
Hierarch
D%pplet ;Dialog D +rame
,eneral/Purpose Containers Intermediate containers that can )e used to displa
components
DPanel
IScrollPane
ISplitPane
DTool)ar
ITa))edPane
Special/Purpose Containers Intermediate containers that pla speci3ic roles in the user
inter3ace5 such as laered panes or internal panes
content pane I#aeredPane
llnternalPane IDes!topPnne
*asic Controls
#o6/level components that e4ist primanl to get input 3rom
the user
I*utton ;Com)o*o4
llist
D"cnu ISSder ISpinner
I+e4t+ield
-nedita)le In3ormation Displas #o6/level components that e4ist solel to give the user
inlormation
D #a)el
;Progress*ar IToodip
Interactive Displas o3 Highl
+ormatted In3ormation
#o6/level components that displa highl 3ormatted
in3ormation that ma )e modi3ied ) the user
;ColorChooser
;+ileChooser
;Ta)le
DTe4t
DTree
#o6/level components5 also called atomic components5 can stand alone in the inter3ace or )e
added to a higher level component in the hierarchU ho6ever5 programmers should not add
components directl to a top/level container5 such as a ;+rame7 The pre3erred method is to use a
special/purpose container to hold components5 called a content pane7 The getContentPane;<
method is used to create an instance o3 the Container class5 similar to a Panel5 6hich docs not
displa a )order7 Content panes hold individual components and are added to5 and there3ore
contained )5 the Z+rame7 Programs that use a content pane e4tend J+ramc and use addO methods
to add components to the instance o3 the content pane7
rJA3@tHl:YTl riiA@iD %ll A0 IA @@@" mA %Q'v@@J in @@ &@ IB 1J&AH@GJ7&&&&J mEJA7 H-5% rD-' HCJE@r@-<
3@limit i6i3n H! H2vrGsD Jm nrlrrr 3it nJrSr3) GAIm t2H68nJl InmnE CrtE7iEr I 8imE E8vFin nEh GilNJJGl it 7inA 1irr it rAenH3I2I ilIn general5 do no I
com)ine complc4 %BT components5 such as "enus and Panels5 in the same container control as S6ing components7 The reason 3or
this is )ecause 6hen light6eight components5 such as S6ing5 overlap 6ith heav6eight components5 such as %BT5 the heav6eight
component is painted on top7
Entering Code to E4tend D +rame
+igure ./. displas the comments5 the import statements5 and the class header 3or the
Classics on D2D program7 #ine 0/0 e4tends the top/level container I+ramc and implements the
%ction#istcncr7
0 8A
2 Chapter .P Classics on D2D
3 ProgrammerP J7 Star!s
4 DateP Novem)er 0:5 :CC.
5 +ilenameP D2D7Java
6 PurposeP This program creates a S6ing inter3ace 3or sorting a O2D collection7
7 2
[
Y i mpor t E ava7 ai vt 7 AU
10 i i r por t E ava7 a6t 7 event 7 AU
11 import EavaA7s6ing7&U
12 import Eava47s6ing7te4t7AP
14 pu)lic class D2D e4tends J+rane implements %ctionlistener
15 0
+I ,-RE ./ .
The 3ollo6ing step enters the code to e4tend I+ramc in the class header5 as ou )egin
coding the Classics on D2D program7
To 9:ten. 5+rae in t-e Class Hea.er
07 Enter the code as sho6n in +igure 7-7; replacing the programmer name and date sho6n
6ith our name and the current date7
Te:tPa. .is"la's t-e beginning *o.e lor t-e DVD *lass (+ig,re 7-<)= T-e *oents; i"ort
stateents; on. *lass -ea.er are .is"la'e.=
@ I & ,i3"i1t73ii1t/t3 %ll Po3isrO "lA m@-G in JliAJn@ && D" DAr tG/Hlt @7@ DG\ i3vid @@2n@ ;t"H2C"tl Dl"K@ 3%" L7I
3altrrti3 hFHivrFl inA@iF6@ml errirrt %vA 3it InnnE 0 nE i8tmn %3 rGEh ninrn 8 3cilrmil @/A3trr) it in 1irr it 6HFtm nEGh r6ndJ;nH 6thv il

You might also like