NetBeans Tutorial For Introduction to Java Programming By Y. Daniel Liang
Thi s t ut or i al appl i es t o Net Beans 6 and 7.
Thi s suppl ement cover s t he f ol l owi ng t opi cs:
Get t i ng St ar t ed wi t h Net Beans ( 1) Cr eat i ng a Pr oj ect ( 2) Cr eat i ng a Cl ass ( 3) Compi l i ng a Cl ass ( 4) Runni ng a J ava Appl i cat i on ( 5) Runni ng Book Exampl es ( 6) Get t i ng Hel p i n Net Beans ( 7) For ci ng a Pr ogr amt o Ter mi nat e ( 8) Usi ng Packages ( 9) Run J ava Appl i cat i ons f r omt he Command Li ne ( 10) Debuggi ng i n Net Beans ( 11) Cr eat i ng and Test i ng J ava Appl et s ( 12)
NOTE: To use t hi s suppl ement wi t h t he t ext , you may cover Sect i ons 1 9 i n t hi s suppl ement af t er Chapt er 1 i n t he t ext , cover Sect i ons 10- 11 i n t hi s suppl ement af t er Chapt er 2 i n t he t ext , and cover Sect i on 12 al ong wi t h Chapt er 17. 0 Introduction Thi s t ut or i al i s f or st udent s who ar e cur r ent l y t aki ng a J ava cour se usi ng Net Beans wi t h I nt r oduct i on t o J ava Pr ogr ammi ng.
You can use t he J DK command l i ne ut i l i t y t o wr i t e J ava pr ogr ams. The J DK command l i ne ut i l i t y consi st s of a set of separ at e pr ogr ams, such as compi l er and i nt er pr et er , each of whi ch i s i nvoked f r oma command l i ne. Besi des t he J DK command l i ne ut i l i t y, t her e ar e mor e t han a dozen J ava devel opment t ool s on t he mar ket t oday, i ncl udi ng Net Beans, J Bui l der , and Ecl i pse. These t ool s suppor t an integrated development environment ( I DE) f or r api dl y devel opi ng J ava pr ogr ams. Edi t i ng, compi l i ng, bui l di ng, debuggi ng, and onl i ne hel p ar e i nt egr at ed i n one gr aphi cal user i nt er f ace. Usi ng t hese t ool s ef f ect i vel y wi l l gr eat l y i ncr ease your pr ogr ammi ng pr oduct i vi t y.
Copyright Y. Daniel Liang, 2012 2
Thi s br i ef t ut or i al wi l l hel p you t o become f ami l i ar wi t h Net Beans 6. Speci f i cal l y, you wi l l l ear n how t o cr eat e pr oj ect s, cr eat e pr ogr ams, compi l e, r un, and debug pr ogr ams. NOTE: Net Beans can r un on any pl at f or mwi t h a J ava Vi r t ual Machi ne. The scr een shot s i n t he t ut or i al ar e t aken f r omWi ndows usi ng Net Beans 6. 1. You can downl oad Net Beans 6. 1 f r om www. net beans. or g. 1 Getting Started with NetBeans Assume t hat you have successf ul l y i nst al l ed Net Beans on your machi ne. St ar t Net Beans f r omWi ndows, Li nux, Mac OS X, or Sol ar i s. The Net Beans mai n wi ndow appear s, as shown i n Fi gur e 1.
Figure 1 The NetBeans main window is the command center for the IDE. The Net Beans mai n wi ndow cont ai ns menus, t ool bar s, pr oj ect pane, f i l es pane, r unt i me pane, navi gat or pane, and ot her panes.
1.1 The Main Menu
The mai n menu i s si mi l ar t o t hat of ot her Wi ndows appl i cat i ons and pr ovi des most of t he commands you need t o use Net Beans, i ncl udi ng t hose f or cr eat i ng, edi t i ng, compi l i ng, r unni ng, and debuggi ng pr ogr ams. The menu i t ems ar e enabl ed and di sabl ed i n r esponse t o t he cur r ent cont ext .
1.2 The Toolbar
The t ool bar pr ovi des but t ons f or sever al f r equent l y used commands on t he menu bar . The t ool bar s ar e enabl ed and di sabl ed i n r esponse t o t he cur r ent cont ext . Cl i cki ng a
Copyright Y. Daniel Liang, 2012 3 t ool bar i s f ast er t han usi ng t he menu bar . For many commands, you al so can use f unct i on keys or keyboar d shor t cut s. For exampl e, you can save a f i l e i n t hr ee ways:
Sel ect Fi l e, Save f r omt he menu bar . Cl i ck t he " save" t ool bar but t on ( ) . Use t he keyboar d shor t cut Ct r l +S. TI P: You can di spl ay a l abel known as ToolTip f or a t ool bar but t on by poi nt i ng t he mouse t o t he but t on wi t hout cl i cki ng. 1.3 Workspaces
A wor kspace i s a col l ect i on of wi ndows t hat ar e per t i nent t o per f or mi ng cer t ai n t ypes of oper at i ons, such as edi t i ng, execut i on, out put , or debuggi ng. The wor kspace wi ndows can be di spl ayed f r omt he Wi ndow menu.
2 Creating a Project A pr oj ect cont ai ns i nf or mat i on about pr ogr ams and t hei r dependent f i l es, and i t al so st or es and mai nt ai ns t he pr oper t i es of t he I DE. To cr eat e and r un a pr ogr am, you have t o f i r st cr eat e a pr oj ect .
Her e ar e t he st eps t o cr eat e a demo pr oj ect :
1. Choose File, New Project t o di spl ay t he New Pr oj ect di al og box, as shown i n Fi gur e 2. 2. Sel ect Gener al i n t he Cat egor i es sect i on and J ava Appl i cat i on i n t he Pr oj ect s sect i on and cl i ck Next t o di spl ay t he New J ava Appl i cat i on di al og box, as shown i n Fi gur e 3. 3. Type demo i n t he Pr oj ect Name f i el d and c: \ mi chael i n Pr oj ect Locat i on f i el d. 4. ( Opt i onal ) You can cr eat e cl asses af t er a pr oj ect i s cr eat ed. Opt i onal l y you may al so cr eat e t he f i r st cl ass when cr eat i ng a new pr oj ect . To do so, check t he Create Main Class box and t ype a cl ass name, say Fi r st , as t he Mai n Cl ass name. 5. Cl i ck Finish t o cr eat e t he pr oj ect . The new pr oj ect i s di spl ayed, as shown i n Fi gur e 4.
Copyright Y. Daniel Liang, 2012 4
Figure 2 The New Project dialog box enables you to specify a project type.
Figure 3 The New Java Application prompts you to enter a project name, location, and a main class name.
Copyright Y. Daniel Liang, 2012 5
Figure 4 A new demo project is created.
3 Creating a Class You can cr eat e any number of cl asses i n a pr oj ect . Her e ar e t he st eps t o cr eat e Wel come. j ava i n Li st i ng 1. 1 f r omChapt er 1 of t he t ext i n t he demo pr oj ect .
1. Ri ght - cl i ck t he t op demo node i n t he pr oj ect pane t o di spl ay a cont ext menu, as shown i n Fi gur e 5. Choose New, Java Class t o di spl ay t he New J ava Cl ass di al og box, as shown i n Fi gur e 6. 2. Type Wel come i n t he Cl ass Name f i el d and sel ect t he Sour ce Packages i n t he Locat i on f i el d. Leave t he Package f i el d bl ank. Thi s wi l l cr eat e a cl ass i n t he def aul t package. ( Not e t hat i t i s not r ecommended t o use t he def aul t package, but i t i s f i ne t o use t he def aul t package t o mat ch t he code i n t he book. Usi ng def aul t package i s appr opr i at e f or new J ava st udent s. Sect i on 10, Usi ng Packages, wi l l i nt r oduce how t o cr eat e a cl ass i n a non- def aul t package. ) 3. Cl i ck Finish t o cr eat e t he Wel come cl ass, as shown i n Fi gur e 7. The sour ce code f i l e Wel come. j ava i s pl aced under t he <def aul t package> node, because you di d not speci f y a package name f or t he cl ass i n Fi gur e 6. 4. Modi f y t he code i n t he Wel come cl ass t o mat ch Li st i ng 1. 1, as shown i n Fi gur e 8.
Copyright Y. Daniel Liang, 2012 6
Figure 5 You can create a new class in a project.
Figure 6 The New Java Class dialog box enables you to specify a class name, location, and package name to create a new class.
Copyright Y. Daniel Liang, 2012 7
Figure 7 A new Java class is created in the project.
Figure 8 The source code for Welcome.java is entered. TI P You can show l i ne number s i n t he Sour ce Edi t or by choosi ng View, Show Line Numbers f r omt he mai n menu. NOTE The sour ce f i l e Wel come. j ava i s st or ed i n c: \ mi chael \ demo\ sr c. 4 Compiling a Class To compi l e Welcome.java, r i ght - cl i ck Wel come. j ava t o di spl ay a cont ext menu and choose Compile File, or si mpl y pr ess F9, as shown i n Fi gur e 9.
Copyright Y. Daniel Liang, 2012 8 The compi l at i on st at us i s di spl ayed i n t he Out put pane, as shown i n Fi gur e 10. I f t her e ar e no synt ax er r or s, t he compiler gener at es a f i l e named Welcome.class, whi ch i s st or ed i n c: \ mi chael \ demo\ bui l d\ cl asses.
Figure 9 The Compile File command in the context menu compiles a source file.
Figure 10 The compilation status is shown in the output pane. Output pane
Copyright Y. Daniel Liang, 2012 9 NOTE: When you compi l e t he f i l e, i t wi l l be aut omat i cal l y saved. NOTE: The i con f or J ava sour ce code i s . A J ava sour ce code may have an addi t i onal i con , whi ch i ndi cat es t hat t he f i l e i s not compi l ed. I f t he cl ass has a mai n met hod, t he i con i s af t er t he cl ass i s compi l ed. TI P: You can get descr i pt i ons of t he node i cons f r omNet Beans Hel p. Choose Help, Help Contents f r omt he mai n wi ndow, and t ype Node I cons under t he Sear ch t ab t o di spl ay t he descr i pt i ons f or t he i cons, as shown i n Fi gur e 11.
Figure 11 You can get descriptions of the node icons in the project pane from NetBeans Help. 5 Running a Java Application To r un Welcome.java, r i ght - cl i ck Wel come. j ava t o di spl ay a cont ext menu and choose Run File, or si mpl y pr ess Shi f t + F6, as shown i n Fi gur e 12. The out put i s di spl ayed i n t he Out put pane, as shown i n Fi gur e 13.
Copyright Y. Daniel Liang, 2012 10
Figure 12 The Run File command in the context menu runs a Java program.
Figure 13 The execution result is shown in the Output pane. Output
Copyright Y. Daniel Liang, 2012 11 NOTE: The Run File command i nvokes t he Compile File command i f t he pr ogr ami s not compi l ed or was modi f i ed af t er t he l ast compi l at i on. 6 Running Book Examples Al l t he exampl es i n t he t ext can be downl oaded f r omt he book s Web si t e. To r un t hem, copy t he sour ce code f i l es i nt o c: \ mi chael \ demo\ sr c. You wi l l see t he J ava sour ce code f i l es appear i ng under t he <def aul t package> node, as shown i n Fi gur e 14. For exampl e, t o r un Comput eLoan. j ava i n Li st i ng 2. 6 i n Chapt er 2, Pr i mi t i ve Dat a Types and Oper at i ons, sel ect Comput eLoan. j ava i n t he pr oj ect pane and pr ess Shi f t + F6.
Figure 14 You can run the examples in the text from the demo project. I f t he pr ogr am( e. g. , I mageAudi oAni mat i on. j ava i n Li st i ng 17. 13 i n Chapt er 17, Appl et s and Mul t i medi a) uses r esour ces ( e. g. , i mage, audi o, t ext ) , you have t o set t he pr oj ect wor ki ng di r ect or y t o t he r esour ce di r ect or i es. Suppose you pl ace t he i mage f i l es under c: \ mi chael \ demo\ bui l d\ cl asses, her e ar e t he st eps t o set t he wor ki ng di r ect or y:
1. Ri ght - cl i ck t he demo pr oj ect node t o di spl ay a cont ext menu and choose Pr oper t i es, as shown i n Fi gur e 15. 2. Sel ect Runni ng Pr oj ect i n t he l ef t sect i on of t he
Copyright Y. Daniel Liang, 2012 12 pr oj ect pr oper t i es di al og box, as shown i n Fi gur e 16. Ent er C: \ mi chael \ demo\ bui l d\ cl asses i n t he Wor ki ng Di r ect or y f i el d. 3. Copy t he i mage and audi o f ol der s t o c: \ mi chael \ demo\ bui l d\ cl asses. Run I mageAudi oAni mat i on. j ava.
Figure 15 You can display Project Properties from the context menu of the project node.
Figure 16
Copyright Y. Daniel Liang, 2012 13 You can set properties for the project in the project properties dialog box. 7 NetBeans's Online Help Net Beans pr ovi des a l ar ge number of document s onl i ne, gi vi ng you a gr eat deal of i nf or mat i on on a var i et y of t opi cs per t ai ni ng t o t he use of Net Beans.
To access onl i ne hel p, choose Help, Help Contents t o di spl ay Net Beans Hel p, as shown i n Fi gur e 17.
Figure 17 All help documents are displayed in NetBeans Help. Net Beans Hel p behaves l i ke a Web br owser and cont ai ns t he t ool bar but t ons, navi gat i on wi ndow, and cont ent wi ndow. The t ool bar but t ons cont ai n f our but t ons: Previous, Next, Print, and Print Setup. The Pr evi ous and Next but t ons l et you go t o t he pr evi ous and next t opi cs i n t he hi st or y l i st . The Pr i nt but t on pr i nt s t he document i n t he cont ent wi ndow. The Pr i nt Set up but t on enabl es you t o set up t he pr i nt l ayout .
The navi gat i on wi ndow cont ai ns t hr ee t abs: Contents, Index, and Search. The Cont ent s t ab di spl ays avai l abl e document s. The t abl e of cont ent s of t he document i s di spl ayed i n a t r ee- l i ke l i st i n t he navi gat i on wi ndow. To vi ew a gi ven t opi c, sel ect t he node i n t he t r ee associ at ed wi t h t he t opi c. Net Beans Hel p di spl ays t he document f or t he t opi c i n
Copyright Y. Daniel Liang, 2012 14 t he cont ent wi ndow.
The I ndex t ab shows t he i ndex ent r i es f or t he cur r ent document . The Sear ch t ab shows t he combi ned i ndex ent r i es f or al l t he avai l abl e document s i n Net Beans.
8 Forcing a Program to Terminate I f a pr ogr amdoes not t er mi nat e due t o a l ogi c er r or , you can f or ce i t t o t er mi nat e by cl i cki ng t he St op i con, as shown i n Fi gur e 18.
Figure 18 You can force a program to terminate from the runtime pane. 9 Using Packages (Optional) The Wel come cl ass cr eat ed i n Sect i on 3, Cr eat i ng a Cl ass, does not have t he package st at ement . I f you want t o cr eat e cl asses wi t h t he package st at ement , you need t o ent er a package name i n t he New J ava Cl ass wi zar d. Her e ar e t he st eps t o cr eat e a new cl ass Wel come i n t he package named chapt er 1:
1. I n t he cont ext menu of t he demo pr oj ect , choose New, Java Class t o di spl ay t he New J ava Cl ass wi zar d, as shown i n Fi gur e 19. 2. Type chapt er 1 i n t he Package f i el d and cl i ck Finish t o cr eat e t he t empl at e f or t he Wel come cl ass, as shown i n Fi gur e 20. 3. Wel come. j ava i s cr eat ed under package chapt er 1. The
Copyright Y. Daniel Liang, 2012 15 f i r st st at ement i n t he sour ce code i s
package chapt er 1;
Modi f y Wel come. j ava t o mat ch t he code i n Li st i ng 1. 1, as shown i n Fi gur e 21. 4. Choose Run File f r omt he cont ext menu of Wel come. j ava i n t he pr oj ect pane t o r un t he pr ogr am.
Figure 19 You can enter a package name to create a class.
Figure 20 The new Welcome class is created under package chapter1. Package chapter1 Package statement
Copyright Y. Daniel Liang, 2012 16
Figure 21 The modified Welcome.java is shown in the Source Editor. NOTE: The package i n J ava cor r esponds t o t he di r ect or y i n t he f i l e syst em. chapter1 i s al so a di r ect or y. Wel come. j ava i s st or ed i n c: \ mi chael \ demo\ sr c\ chapt er 1 and Wel come. cl ass i s st or ed i n c: \ mi chael \ demo\ bui l d\ cl asses\ chapt er 1. 10 Run Java Applications from the Command Line So f ar you have r un pr ogr ams i n t he Net Beans I DE. You can al so r un pr ogr amst andal one di r ect l y f r omt he oper at i ng syst em. Her e ar e t he st eps i n r unni ng t he Welcome appl i cat i on wi t h t he def aul t package cr eat ed i n Sect i on 3 f r omt he DOS pr ompt . 1. St ar t a DOS wi ndow by cl i cki ng t he Wi ndows St ar t but t on, Pr ogr ams, MS- DOS Pr ompt i n Wi ndows. 2. Type t he f ol l owi ng commands t o set up t he pr oper envi r onment var i abl es f or r unni ng J ava pr ogr ams i n t he DOS envi r onment i n Wi ndows: set path=%path%;c:\Program Files\java\jdk1.6.0\bin set classpath=.;%classpath% 3. Type cd c:\michael\demo\build\classes t o change t he di r ect or y t o c:\michael\demo\build\classes 4. Type java Welcome t o r un t he pr ogr am. A sampl e r un of t he out put i s shown i n Fi gur e 22.
Figure 22
Copyright Y. Daniel Liang, 2012 17 You can run the Java program from the DOS prompt using the java command. NOTE: To r un Wel come i n t he chapt er 1 package cr eat ed i n Sect i on 11, t ype java chapter1.Welcome f r omt he c:\michael\demo\build\classes di r ect or y, as shown i n Fi gur e 23.
Figure 23 If a class has the package statement, you have to specify its full path, including the package name. 11 Debugging in NetBeans The debugger ut i l i t y i s i nt egr at ed i n Net Beans. You can pi npoi nt bugs i n your pr ogr amwi t h t he hel p of t he Net Beans debugger wi t hout l eavi ng t he I DE. The Net Beans debugger enabl es you t o set br eakpoi nt s and execut e pr ogr ams l i ne by l i ne. As your pr ogr amexecut es, you can wat ch t he val ues st or ed i n var i abl es, obser ve whi ch met hods ar e bei ng cal l ed, and know what event s have occur r ed i n t he pr ogr am. The debugger i s an i ndi spensabl e, power f ul t ool t hat boost s your pr ogr ammi ng pr oduct i vi t y. I t i s al so a val uabl e t ool f or l ear ni ng J ava. I t hel ps you under st and how a pr ogr ami s execut ed. 11.1 Setting Breakpoints
You can execut e a pr ogr aml i ne by l i ne t o t r ace i t , but t hi s i s t i me- consumi ng i f you ar e debuggi ng a l ar ge pr ogr am. Of t en, you know t hat some par t s of t he pr ogr amwor k f i ne. I t makes no sense t o t r ace t hese par t s when you onl y need t o t r ace t he l i nes of code t hat ar e l i kel y t o have bugs. I n cases of t hi s ki nd, you can use br eakpoi nt s.
A breakpoint i s a st op si gn pl aced on a l i ne of sour ce code t hat t el l s t he debugger t o pause when t hi s l i ne i s encount er ed. The debugger execut es ever y l i ne unt i l i t encount er s a br eakpoi nt . You can t hen t r ace t he par t of t he pr ogr amat t he br eakpoi nt , qui ckl y movi ng over t he sect i ons t hat wor k cor r ect l y and concent r at i ng on t hose causi ng
Copyright Y. Daniel Liang, 2012 18 pr obl ems.
Ther e ar e sever al ways t o set a br eakpoi nt . One qui ck way i s t o cl i ck t he cut t er of t he l i ne on whi ch you want t o put a br eakpoi nt . You wi l l see t he l i ne hi ghl i ght ed. You al so can set br eakpoi nt s by choosi ng Run, New Breakpoint. To r emove a br eakpoi nt , si mpl y cl i ck t he cut t er of t he l i ne. When debuggi ng a pr ogr am, you can set as many br eakpoi nt s as you want , and can r emove br eakpoi nt s at any t i me dur i ng debuggi ng. The pr oj ect r et ai ns t he br eakpoi nt s when you exi t t he pr oj ect . They ar e r est or ed when you r eopen i t .
11.2 Starting the Debugger
Let us use Li st i ng 2. 8, ShowCur r ent Ti me. j ava, t o demonst r at e debuggi ng. Cr eat e a new cl ass named ShowCur r ent Ti me i n t he def aul t package i n t he demo pr oj ect , as shown i n Fi gur e 24.
Figure 24 ShowCurrentTime.java is created in the project. Ther e ar e sever al ways t o st ar t t he debugger . A si mpl e way i s as f ol l ows:
1. Act i vat e ShowCur r ent Ti me. j ava i n t he edi t or pane. 2. Set a br eakpoi nt at wher e you want your pr ogr amt o pause, say Li ne 6. A br eakpoi nt can be set by cl i cki ng t he cut t er of t he l i ne, as shown i n Fi gur e 25. You can r emove i t by cl i cki ng on t he cut t er of l i ne agai n. 3. I n t he cont ext menu of ShowCur r ent Ti me. j ava i n t he pr oj ect pane, choose Debug File. I f t he pr ogr am compi l es pr oper l y, an out put pane and debug pane wi l l be di spl ayed, as shown i n Fi gur e 26. I f t he debug
Copyright Y. Daniel Liang, 2012 19 pane i s not shown, choose Window, Debugging, Local Variables t o di spl ay i t .
Figure 25 Clicking the line number sets/removes a breakpoint at the line in the source code.
Figure 26 The debugger starts to run ShowCurrentTime.java.
11.3 Controlling Program Execution
The pr ogr ampauses at t he br eakpoi nt . The paused l i ne i s hi ghl i ght ed i n gr een. Thi s l i ne i s al so cal l ed t he current execution point, whi ch poi nt s t o next st at ement t o be execut ed by t he debugger .
When t he pr ogr ampauses, you can i ssue debuggi ng commands t o cont r ol t he execut i on of t he pr ogr am. You al so can i nspect Debugging toolbar buttons Debugging window
Copyright Y. Daniel Liang, 2012 20 or modi f y t he val ues of var i abl es i n t he pr ogr am.
When Net Beans i s i n t he debuggi ng mode, t he t ool bar but t ons f or debuggi ng ar e di spl ayed, as shown i n Fi gur e 26. The t ool bar but t on commands al so appear i n t he Run menu ( see Fi gur e 27) . Her e ar e t he commands f or cont r ol l i ng pr ogr am execut i on:
Start begi ns t o debug t he cur r ent pr ogr am. Finish ends t he cur r ent debuggi ng sessi on. Attach opens a di al og box i n whi ch you can connect t he debugger t o an appl i cat i on on anot her vi r t ual machi ne. Thi s i s usef ul f or r emot e debuggi ng i n di st r i but ed syst ems. Pause t empor ar i l y st ops execut i on of a pr ogr am. Run to Cursor r uns t he pr ogr am, st ar t i ng f r omt he cur r ent execut i on poi nt , and pauses and pl aces t he execut i on poi nt ei t her on t he l i ne of code cont ai ni ng t he cur sor or at a br eakpoi nt . Step Over execut es a si ngl e st at ement . I f t he st at ement cont ai ns a cal l t o a met hod, t he ent i r e met hod i s execut ed wi t hout st eppi ng t hr ough i t . Step Into execut es a si ngl e st at ement or st eps i nt o a met hod. Step Out execut es al l t he st at ement s i n t he cur r ent met hod and r et ur ns t o i t s cal l er . Continue r esumes t he execut i on of a paused pr ogr am.
Copyright Y. Daniel Liang, 2012 21 Figure 27 The debugging commands appear under the Run menu. 11.4 The Debugger Window
The Debugger wi ndow has t abbed panes f or Local Var i abl es, Wat ches, and Cal l St ack, as shown i n Fi gur e 26. Ot her t abbed such as Br eakpoi nt s, Sessi ons, and Thr eads can be added by choosi ng Wi ndow, Debuggi ng f r omt he mai n menu. These panes can be sel ect ed or desel ect ed. The Sessions pane l i st s t he cur r ent debug sessi ons. The Breakpoints pane l i st s al l t he br eakpoi nt s you have set . The Threads pane l i st s t hr eads and t hr ead gr oups i n t he cur r ent debuggi ng pr ocess. The Call Stack pane l i st s t he met hod cal l s t hat t he pr ocess has made si nce i t began r unni ng. The Watches pane l i st s t he var i abl es and expr essi ons t hat ar e under cont i nuous wat ch. The Local Variables pane shows al l t he var i abl es bef or e t he cur r ent execut i on poi nt i n a l ocal met hod. The Classes pane l i st s al l t he cl asses t hat have been l oaded by t he pr ocess bei ng debugged.
11.5 Examining and Modifying Data Values Among t he most power f ul f eat ur es of an i nt egr at ed debugger i s i t s capabi l i t y t o r eveal cur r ent dat a val ues and enabl e pr ogr ammer s t o modi f y val ues dur i ng debuggi ng. You can exami ne t he val ues of var i abl es, ar r ay i t ems, and obj ect s, or t he val ues of t he par amet er s passed i n a met hod cal l . You al so can modi f y a var i abl e val ue i f you want t o t r y a new val ue t o cont i nue debuggi ng wi t hout r est ar t i ng t he pr ogr am.
The Local Var i abl es pane l i st s al l var i abl es accessi bl e at t he cur r ent execut i on poi nt . The Wat ches pane l i st s t he sel ect ed var i abl es.
12.5.1 The Add Watch Command Net Beans pr ovi des t he Add Wat ch command t o enabl e you t o add var i abl es t o t he Wat ches pane i n t he Debugger wi ndow. To add t he var i abl e t ot al Mi l l i seconds t o t he Wat ch vi ew, per f or m t he f ol l owi ng st eps: 1. Suppose t he execut i on poi nt i s cur r ent l y at t he f i r st l i ne i n t he mai n met hod. Hi ghl i ght t ot al Mi l l i seconds i n t he Sour ce Edi t or and r i ght - cl i ck t he mouse t o r eveal a cont ext menu. 2. Choose New Watch i n t he cont ext menu t o br i ng up a di al og box, as shown i n Fi gur e 28. Cl i ck OK t o add t ot al Mi l l i seconds t o t he Wat ch l i st . 3. Choose t he Wat ches t ab i n t he Debugger wi ndow. The
Copyright Y. Daniel Liang, 2012 22 var i abl e al ong wi t h i t s cont ent i s shown i n Fi gur e 29. 4. Choose Debug, Step Over t o obser ve t he changi ng val ue of t ot al Mi l l i seconds i n t he Wat ches pane.
Figure 28 The New Watch dialog box enables you to add a variable or an expression to the Watch view.
Figure 29 The variable totalMilliseconds was added to the Watches tab. NOTE: You can al so add expr essi ons such as i > 0 t o t he Wat ches t ab f r omt he Add New Wat ch di al og box. 11.5.2 Modifying Variables You can modi f y var i abl es f r omt he Wat ches pane or t he Var i abl es pane. For i nst ance, t o change t he val ue f or t ot al Mi l l i seconds, ent er a new val ue, say 1000, i n t he val ue f i el d f or t he var i abl e and pr ess t he Ent er key ( See Fi gur e 30) .
Copyright Y. Daniel Liang, 2012 23
Figure 30 The value for variable totalMilliseconds is changed to 1000. 12 Creating and Testing Java Applets I n t he pr ecedi ng sect i on, you l ear ned how t o cr eat e, compi l e, and execut e a J ava pr ogr am. Appl et s ar e speci al t ype of J ava pr ogr am. Net Beans pr ovi des t he Appl et wi zar d t o cr eat e J ava appl et s.
12.1 Creating a Java Applet
Let us use Wel comeAppl et . j ava i n Li st i ng 16. 1 i n t he t ext t o demonst r at e how t o cr eat e an appl et : 1. I n t he cont ext menu of t he demo pr oj ect , choose New, File/Folder ( See Fi gur e 31) t o di spl ay t he New Fi l e wi zar d, as shown i n Fi gur e 32.
2. Sel ect J Appl et under t he J ava Cl asses node and cl i ck Next t o di spl ay t he Appl et wi zar d, as shown i n Fi gur e 33.
3. Type Wel comeAppl et i n t he Cl ass Name f i el d, and cl i ck Finish t o cr eat e t he t empl at e f or t he appl et , as shown i n Fi gur e 34.
4. Modi f y t he code i n t he Wel comeAppl et cl ass t o di spl ay a t ext on a l abel , and pl ace t he l abel i n t he cent er of t he appl et , as shown i n Fi gur e 35. The code i s t he same as Li st i ng 16. 1 i n t he t ext .
5. Choose Run File i n t he cont ext menu of Wel comeAppl et . j ava t o r un t he appl et . Net Beans aut omat i cal l y gener at es Wel comeAppl et . ht ml i n C: \ mi chael \ demo\ bui l d and r uns t he appl et usi ng t he appl et vi ewer , as shown i n Fi gur e 36. A new value is entered
Copyright Y. Daniel Liang, 2012 24
Figure 31 You can create an applet from the File/Folder menu.
Figure 32 You can choose an applet template from the New File wizard.
Copyright Y. Daniel Liang, 2012 25
Figure 33 You can create an applet from the Applet wizard.
Figure 34 The Applet wizard generates the template for the applet.
Figure 35 The source code of the applet was modified.
Copyright Y. Daniel Liang, 2012 26
Figure 36 The WelcomeApplet program runs from the applet viewer. 12.2 Viewing Applets from a Web Browser
Appl et s ar e event ual l y di spl ayed i n a Web br owser . Usi ng t he appl et vi ewer , you do not need t o st ar t a Web br owser . The appl et vi ewer f unct i ons as a br owser . I t i s conveni ent f or t est i ng appl et s dur i ng devel opment . However , you shoul d al so t est t he appl et s f r oma Web br owser bef or e depl oyi ng t hemon a Web si t e. To di spl ay an appl et f r oma Web br owser , open t he appl et s HTML f i l e ( i . e. , Wel comeAppl et . ht ml ) . I t s out put i s shown i n Fi gur e 37.
Figure 37 The WelcomeApplet program is displayed in Internet Explorer.