Building a Java Desktop Database Application - NetBeans IDE Tutorial

  

Building a Java Desktop Database Application - NetBeans IDE Tutorial http://netbeans.org/kb/70/java/gui-db.html?print=yes

Building a Java Deskt op Dat abase Applicat ion

  N ot e : JSR- 29 6 ( Swing Ap plicat ion Fr am ewor k) is no lon ger developed an d will not becom e par t of t he official Java

  Developm ent Kit as was or iginally planned. You can st ill u se t h e Swing Applicat ion Fr am ework libr ar y as it is, but n o f ur t h er developm en t is ex pect ed. I f you are looking for a Swing- based applicat ion fr am ew or k, con sider u sing t h e Net Bean s Plat form , wh ich is a f ull- feat ur ed plat form suit able for creat in g com plex and scalable deskt op applicat ion s. The Plat form con t ain s API s t hat sim plify t he handlin g of w in dows, act ion s, files, an d m an y ot h er t ypical applicat ion elem en t s. Refer t o t h e Net Bean s Plat form CRUD Applicat ion Tu t orial, w hich describes a sim ilar scen ar io. This t ut or ial shows h ow t o cr eat e a deskt op Java applicat ion t hr ough which you can access and updat e a dat ab ase. Th e t ut or ial t akes advan t age of support in Net Beans I DE f or t he following t echnologies:

  The Java Persist en ce API ( JPA) , wh ich helps you u se Java code t o in t er act wit h dat abases. Beans Binding ( JSR- 295 ) , w hich pr ovides a way for differ en t JavaBean s com ponent s t o have proper t y values t hat ar e synchr onized wit h each ot h er. For exam ple, you can use beans binding t o keep t he valu es of cells in a JTable visual com pon ent in synch wit h t he values of f ields in an ent it y class. ( I n t u rn , t h e ent it y class r epr esent s t he dat abase t able. ) The Swing Applicat ion Fr am ewor k ( JSR- 2 96 ) , wh ich pr ovides som e usef ul building blocks for qu ickly cr eat ing deskt op app licat ions.

  We will cr eat e a dat abase CRUD ( creat e, read, updat e, delet e) applicat ion wit h a cu st om com ponent used f or visualizing t he dat a ( car design pr eview ) . This t ut or ial is lar gely based on a scr eencast t hat w as based on a developm en t build of a previous ver sion of t h e I DE. Som e of t he u ser in t er face has ch anged sin ce t h at dem o w as m ade, so you m igh t not ice som e differen ces bet w een t h is t ut orial and t h e dem o. You can view t he dem o ( about 9 m inut es) now or down load a zip of t he dem o .

  

Ex pe ct e d du r a t io n: 4 5 m in u t e s

Con t e nt s Set t in g Up t he Dat abase St ar t in g t he Ser ver and Creat in g a Dat abase Conn ect ing t o t he Dat abase Creat in g t he Applicat ion Ru nnin g t he Gen erat ed Applicat ion Reviewing t he Gener at ed Par t s of t h e Applicat ion Adding Addit ion al Cont r ols Binding Cont rols t o Valu es in t he Table Set t in g up a Cust om Com pon ent Bu ilding and Deploying t he Applicat ion Next St eps To com p le t e t h is t u t or ia l, y ou n e e d t h e soft w a r e a n d r e sou r ce s list e d in t h e follo w in g t a b le .

  Soft w a r e or Re sou rce Ve r sion Re qu ir e d Net Beans I DE ver sion 6 .9 or 7. 0 Java Developm ent Kit ( JDK) ver sion 6

  Java DB dat abase ser ver ( Java DB is included w it h JDK 6)

  Car d at abase SQL scr ipt Car Preview pr oj ect Set t ing Up t he Dat abase Befor e y ou begin cr eat ing a deskt op CRUD ( cr eat e, r ead, updat e, delet e) applicat ion in t he I DE, you should alr eady have t he I DE connect ed t o t h e dat abase t hat your applicat ion will use. Having t h is connect ion set up in advan ce will allow you t o t ake advant age of I DE f eat ur es t hat aut om at e t h e binding of t he dat abase t o your applicat ion .

  I n t h is t ut orial, we pr ovide inst r uct ions for using a Java DB dat abase, since t her e is a conven ient int er face f or st ar t ing and st oppin g t he Java DB dat abase ser ver fr om t he I DE. However , you can use a differ en t dat abase ser ver wit hout t oo m u ch difficult y. First ver ify t hat you have Java DB r egist ered in t he I DE. Java DB is aut om at ically regist ered in t he I DE in a num ber of cases, such as when you h ave t he GlassFish applicat ion ser ver r egist er ed in t he I DE or when you are ru nnin g on JDK 6. I f Java DB is not r egist ered in t he I DE, r egist er it m anually.

  To v e r ify w h e t h e r Ja v a D B is r e gist e r e d in t he I D E: Open t he Services win dow. 1 .

  Righ t - click t he Dat abases > Java DB n ode and choose Pr oper t ies. I f Java DB is regist er ed, t h e Java DB I n st allat ion an d Dat abase Locat ion f ields w ill b e filled in. I f Java DB is not r egist ered, fill in t he f ollow in g t wo fields: Ja v a DB I n st a lla t io n. Ent er t h e pat h t o t he dat abase ser ver .

  D a t a b a se Loca t ion . En t er t he folder wher e you want t he dat abases t o be st or ed.

  2 .

  Click OK. 3 .

  St art ing t he Server and Creat ing a Dat abase Once Java DB is regist er ed wit h t he I DE, you can easily st ar t and st op t he dat abase ser ver , as well as cr eat e a new dat abase.

  To st a rt t h e da t a ba se se r v e r in t h e I D E: I n t he Serv ices win dow, r ight - click Dat abases > Java DB and choose St art Ser ver .

  I f you do n ot alr eady have a locat ion set for t he dat abase, t h e Set Dat abase Locat ion dialog box appear s. Ent er a locat ion for t he dat abase ser ver t o st ore t h e dat abases. You can creat e a n ew f older t her e if you wish. Once t he ser ver is st ar t ed, Java DB Dat abase Pr ocess t ab opens in t he Ou t pu t window an d displays a m essage sim ilar t h e following :

  Apache Derby Network Server - 10.6.2.1 - (999685) started and ready to accept connections on port 1527 To cr e a t e t he da t a ba se : I n t he Serv ices win dow, r ight - click Dat abases > Java DB and choose Cr eat e Dat abase. 1 .

  For t he Dat abase Nam e t ext field, t ype car_database . Also set t he User Nam e an d Passw or d t o nbuser . 2 . Click OK. 3 .

  So far, w e have successf ully st art ed t he t h e dat abase ser ver and creat ed a dat abase. However , we st ill need t o con nect t o t he new dat abase bef or e w e can st ar t wor kin g wit h it in t h e I DE. To con nect t o t he car_database dat abase: Swit ch t o t h e Ser vices window ( Ct r l+ 5) an d expan d t he Dat abases node t o see you r new dat abase. 1 . Righ t - click t he dat abase connect ion node ( jdbc:derby://localhost:1527/car_database[nbuser on NBUSER] ) an d choose Con nect . The Connect ion node icon sh ou ld now appear as n o longer br oken ( ) , wh ich sig nifies t hat t he connect ion was su ccessful. 2 .

  

Building a Java Desktop Database Application - NetBeans IDE Tutorial http://netbeans.org/kb/70/java/gui-db.html?print=yes

Connect ing t o t he Dat abase

Building a Java Desktop Database Application - NetBeans IDE Tutorial http://netbeans.org/kb/70/java/gui-db.html?print=yes

  3 . Exp and t he conn ect ion n ode and n ot e t hat t h ere ar e sever al schem a subn odes. Righ t - click t he APP node and choose Set as Default Sch em a. 4 . Exp and t he APP sch em a nod e, right - click it s Tables su bnode, and ch oose Execut e Com m an d. 5 . Copy t he cont en t s of t he car. sql file and past e t hem int o t h e SQL Com m an d t ab of t he SQL Sour ce Edit or .

  This is t he SQL scr ipt which w ill populat e t h e dat abase wit h dat a about car s. 6 . Click t he Run SQL bu t t on ( ) in t h e t oolb ar of t he SQL Edit or t o ru n t he scr ipt . 7 . Righ t - click t he Tables node of t he APP schem a and choose Refr esh t o view t he new Car t able.

Creat ing t he Applicat ion 1 . Choose File > New Pr oj ect

  2 . I n t he first panel of t he wizar d, expand t h e Java cat eg or y an d select t h e Java Deskt op Applicat ion t em plat e. Click Next .

  The Java Deskt op Applicat ion t em plat e pr ovides m any basics of a visu al applicat ion, including basic m enu it em s an d com m an ds. 3 . I n t he Disclaim er page, click Next . 4 . I n t he Nam e and Locat ion page of t h e wizar d, do t he f ollowin g t hings:

  a. I n t he Pr oj ect Nam e field, t ype CarsApp . The value of t h is field set s t h e disp lay nam e f or t he p roj ect in t he Pr oj ect s window.

  b. Select t h e Set as Main Proj ect ch eckbox.

  c. ( Opt ional) Edit t he Pr oj ect Locat ion field t o chan ge t he locat ion of y our pr oj ect m et adat a.

  d. ( Opt ional) Select t h e Use Dedicat ed Folder for St oring Lib raries checkbox an d specif y t he locat ion f or t he libr ar ies fold er. See Shar ing Pr oj ect Librar ies f or m or e inf or m at ion on t his opt ion .

  e. I n t he Choose Applicat ion Shell field, select Dat abase Applicat ion.

  f. Click Next . 5 . I n t he Mast er Table page of t he w izard, select t h e dat ab ase con nect ion f or t he CAR dat abase. The list in g for t h e dat abase should look som et hing like t he following: jdbc:derby://localhost:1527/car_database[nbuser on NBUSER] 6 . Select t he bot t om five colum n nam es ( beginning wit h SUN_ROOF and ending wit h MODERNNESS ) and click t he < but t on t o m ove t hem t o t he left colum n. Click Next .

  

Building a Java Desktop Database Application - NetBeans IDE Tutorial http://netbeans.org/kb/70/java/gui-db.html?print=yes

  7 . I n t he Det ail Opt ions pan el, click Fin ish.

  The wizar d t hen gener at es a basic user int er face w it h a t able and a dat abase conn ect ion. This m ight t ake a f ew secon ds as t he I DE gener at es t he pr oj ect and t he code.

  Running t he Generat ed Applicat ion

  At t his poin t , you have a basic r unn ing applicat ion wit h a gr aphical u ser in t er face ( GUI ) t hat h as t he follow ing feat ur es: Abilit y t o view and m odify values in five colu m ns of t he CAR dat abase.

  Basic m en u it em s. Persist en ce of it s w indow st at e bet ween session s. Wh en you close t h e applicat ion, t he window posit ion an d size ar e r em em ber ed. So w hen you r eopen t he applicat ion , t h e win dow open s in t he sam e posit ion as it was when you closed it .

  An Abou t dialog box, which you can easily cust om ize.

  .properties f iles cont aining t h e labels in t he user int er face. Usin g .properties files is a good way t o keep t he logic of

  your code separ at e fr om t he t ext t hat appear s in t h e user int er face of your applicat ion. Such separat ion is usef ul for m aking it easier t o localize you r progr am , am on g ot her reasons.

  To se e som e o f t h e f e a t u r e s t h a t a r e a lr e a dy b u ilt in t o t h e a p plica t ion , follow t h e se st e p s: 1 . Righ t - click t he proj ect 's node and choose Run .

  Aft er a few seconds, t he applicat ion st art s an d a window called Dat abase Applicat ion Exam ple appear s. Th is w indow con t ain s a t able an d sever al con t r ols t hat enable you t o edit t he CARS dat abase. 2 . Select t he f ir st recor d in t he t able ( f or t he Acu ra) . 3 . Select t he Price t ext field and r eplace t he exist ing value wit h 46999 . Then press Ent er.

  The value sh ould appear u pdat ed in t h e t able. ( However, t hat value will not be r eflect ed in t he dat abase u nt il you click Save.) Sim ilar ly, you can u pdat e an y ot h er values in t h e t able.

  4 . Click New t o cr eat e a new r ecor d. Then fill in valu es for each of t he fields ( Make, Mod el, Price, Body St yle, Color) . For exam ple, you can fill in Trabant , Classic , 1000 , wagon , an d blue . Click Save t o save t he ent r y in t he dat abase. 5 . Click t he Dat abase Applicat ion Exam ple t it le bar and dr ag t h e applicat ion t o a differ ent place on you r scr een . 6 . Click t he left bor der of t he Dat abase Applicat ion Exam p le window and dr ag t o t he lef t t o incr ease t he size of t h e window. 7 . I n t he Dat abase Applicat ion Exam ple m enu bar , choose File > Exit . 8 . I n t he I DE, r ight - click t h e pr oj ect 's nod e an d choose Ru n Proj ect .

  The Dat abase Applicat ion Exam ple window w ill open in t he sam e size an d posit ion it w as in w hen you closed t he applicat ion .

Reviewing t he Generat ed Part s of t he Applicat ion

  The conn ect ion bet ween t he m ast er t able ( a JTable com pon ent ) and t he dat abase is hand led wit h a com binat ion of t he following m echanism s, all of w hich have been generat ed by t he I DE: The Car.java ent it y class, which is used t o r ead and wr it e d at a t o t h e CAR dat abase t able. Ent it y classes are a special t ype of class t h at en able you t o int eract w it h dat abases t hr ough Java code. Ent it y classes use Java an not at ion s t o m ap class fields t o dat abase colu m ns. The META-INF/persistence.xml f ile, w hich def in es a connect ion bet ween t h e dat abase an d t he en t it y class. Th is f ile is also known as t he per sist en ce unit . Usin g bean s bin ding t o conn ect t he pr oper t ies of t h e en t it y class wit h t he pr oper t ies of t he JTable com ponent . Beans binding is a new t echn ology based on JSR 29 5 and w hich will pr obably be included in a fut ur e Java SE r elease. The entityManager , query , an d list obj ect s, which are defined in t he CarsView class an d wh ich ar e list ed in t he I nspect or window.

  The ent it y m anager obj ect is used t o r et r ieve and com m it dat a wit h in t he defined persist ence un it scope.

Building a Java Desktop Database Application - NetBeans IDE Tutorial http://netbeans.org/kb/70/java/gui-db.html?print=yes

  The qu er y obj ect defines how t he par t icular dat a collect ion is r et r ieved fr om t he ent it y m anager . ( You can ch ange t h e way t hat t he quer y obj ect w or ks by select in g t he qu er y obj ect in t h e I nspect or window and changing t he query pr oper t y in t he pr oper t y sheet . The query proper t y uses JPA qu ery language.

  The list obj ect is an obser vable collect ion t hat holds t he dat a fr om t he qu er y. An obser vable collect ion is a special kin d of collect ion on w hich you can place a list ener t o find out w hen chang es t o t he collect ion have been m ade. Usin g t he I nspect or win dow and t h e pr opert y sheet , you can follow t hese st eps t o see how t he JTable is boun d t o dat a: 1 . I n t he I n spect or w in dow , select t he m ainPanel[ JPanel] > m ast erScr ollPane [ ScrollPan e] > m ast er Table [ JTable] n ode. Then click t h e Binding t ab in t he Pr oper t ies window. 2 . Look at t h e elem en t s p roper t y t o con firm t hat it is bou nd t o a list . 3 . Click t he ellipsis [ . . .] bu t t on t o open t h e Bind m ast erTable.elem ent s cu st om izer, w her e you can fu rt her cu st om ize t he binding bet ween t h e t able and t he dat abase. For exam ple, you can see t hat t he cu st om izer enab les you t o specify wh ich t able colu m ns are bou nd. Besides t he Binding cat egory in pr opert y sheet you can also use t he Bind m enu in con t ext m enu.

  Adding Addit ional Cont rols

  We will now add cont r ols t o t he form for som e of t h e at t ribu t es w e init ially exclu ded. I n st ead of using a t able, we will add JSlid er com pon ent s ( f or t he t ir e size an d m oder nness at t r ibu t es) and JCh eckbox com pon ent s ( f or t he su n r oof and t h e spoiler ) .

  Follow t h e se st e p s t o a dd t h e a d dit ion a l com po ne n t s:

  1 . Add t he fir st slider by clicking t h e Slider but t on in t he Palet t e window and t hen clicking in t he f or m j ust above t he New but t on . Before clicking in t h e for m t o in sert t he slider , m ake sur e t hat n o h or izon t al slot t ed gu idin g lines ar e sh own . These lines indicat e t hat t h e slider w ill be inser t ed in t he sam e line as t he f ields or t he bu t t ons. See t he f igur e below t o see where you should dr op t he slider int o t he form .

  N ot e : I f you dr op t he com ponen t in a place you do not wan t and t hu s cause sever al u ndesir ed layout chan ges, y ou can u se t he Undo com m an d t o r ever se t he ch anges. Choose Ed it > Un do or pr ess Ct r l- Z.

  2 . I f n ecessar y, st r et ch t he slider t o t h e left t o align it wit h t he left side of t he t ext f ield com ponent s. 3 . St ret ch t h e slider t o t he r ight t o span t he whole for m w idt h. 4 . Add a label t o t he left of t h e slider and set it s t ex t t o Tire Size . ( Click t he label t o m ake it edit able. ) 5 . Add anot h er slider below t he first slider, and adj ust it s w idt h and alig nm en t wher e n ecessary.

  6 . Add anot h er label below t he Tir e Size label and set it s t ext t o Modernness . 7 . Add t wo checkboxes below t he slider s. Set t heir t ext t o Spoiler an d Sun Roof . ( Make t h e display t ext edit able by clickin g t he ch eckb ox once, pau sin g, and t hen clicking t h e checkbox again . You can also r igh t - click t he checkbox and ch oose Edit

  Text .) The form should look like t he scr eenshot show n below.

  

Building a Java Desktop Database Application - NetBeans IDE Tutorial http://netbeans.org/kb/70/java/gui-db.html?print=yes

Binding Cont rols t o Values in t he Table

  We will now use beans bindin g feat u res t o t ie t h e valu es show n in t able cells t o t he st at e of t he cont r ols we have added. This will allow you t o chan ge t he values of cells in t he t able by using t h e sliders and checkboxes.

  To b in d t h e slid e r s t o t h e ir cor r e spon d in g t a b le e le m e n t s: 1 . I n t he for m , r ight - click t he Tir e Size slider and ch oose Bind > value.

  2 . I n t he Binding Sour ce dr op- dow n list of t h e Binding dialog b ox, select masterTable . 3 . I n t he Binding Expression dr op- dow n list , select t h e selectedElement > tiresize n ode and click OK.

  Click OK t o exit t he Bind dialog box. 4 . I n t he for m , r ight - click t he Moder nness slider and choose Bind > valu e. 5 . I n t he Binding Sour ce dr op- dow n list of t h e Binding dialog b ox, select masterTable . 6 . I n t he Binding Expression dr op- dow n list , select selectedElement > modernness and click OK. 7 . Click OK t o exit t he Bind dialog box. 8 .

  To b in d t h e ch e ck box e s t o t h e ir cor r e spon d in g t a b le e le m e n t s: I n t he for m , r ight - click t he Spoiler ch eck box and choose Bin d > select ed. 1 .

  I n t he Binding Sour ce dr op- dow n list of t h e Binding dialog b ox, select masterTable . 2 . I n t he Binding Expression dr op- dow n list , select selectedElement > spoiler . 3 . Click OK t o exit t he Bind dialog box. 4 . I n t he for m , r ight - click t he Su n Roof checkbox an d choose Bind > select ed. 5 . I n t he Binding Sour ce dr op- dow n list of t h e Binding dialog b ox, select masterTable . 6 . I n t he Binding Expression dr op- dow n list , select selectedElement > sunRoof and click OK. 7 . Click OK t o exit t he Bind dialog box. 8 . You should n ow be able t o change dat abase en t r ies using t he slider and checkboxes.

  To v e r ify t h a t t h e slide r s a n d ch e ck box e s w or k : Open t he Services view. 1 .

  Make sur e t he I DE h as a conn ect ion t o t h e dat abase by r ight - clicking Databases > jdbc:derby;//localhost:1527 /car_database and ch oosin g Conn ect . 2 .

  Righ t - click t he Databases > jdbc:derby;//localhost:1527/car_database > APP > Tables > CAR node and choose View Dat a. 3 .

  Look at t h e SUN_ROOF , SPOILER , TIRE_SIZE , and MODERNNESS valu es for t h e fir st record . 4 .

  

Building a Java Desktop Database Application - NetBeans IDE Tutorial http://netbeans.org/kb/70/java/gui-db.html?print=yes

Building a Java Desktop Database Application - NetBeans IDE Tutorial http://netbeans.org/kb/70/java/gui-db.html?print=yes

  5 . Choose Ru n > Ru n Main Pr oj ect t o execut e t he applicat ion.

  The ru nning applicat ion should look sim ilar t o t h e screenshot sh own below .

  6 . I n t he ru nning applicat ion, select t h e fir st r ecord. 7 . Move t he slider s and ch ange t he checkbox select ions. 8 . Click Sav e t o save t he changes int o t h e dat ab ase. 9 . I n t he Serv ices win dow, u se t he View Dat a com m an d again .

  The values in t h e dat abase should r eflect ch anges t h at you have m ade.

  Set t ing up a Cust om Com ponent

  For an even m or e gr aph ic dem on st r at ion of bean s bin ding in act ion, let 's add a cust om com ponen t t h at will paint a pr eview of t he select ed car . We will bind pr oper t ies of t his com ponent t o corr espon ding t able elem ent s. Th en w hen you r un t h e applicat ion again, t he car pr eview w ill be m od if ied as you change t he select ed row and change t he values of t h e var iou s fields.

  To m a k e t h e Ca r Pr e v ie w com pon e n t a v a ila b le f or t h e Ca r sAp p p r oj e ct : 1 . I f you h ave n ot alr eady don e so, down load t he CarPreview.zip f ile .

  2 . Usin g a st and ar d zip t ool, ext r act t he archives of t he zip file. 3 . Choose File > Open Proj ect and navigat e in t o t he ext r act ed con t ent s of t h e zip file and select t h e Car Pr eview pr oj ect . 4 . Click Open Proj ect .

  The proj ect open s in t he I DE. 5 . Righ t - click t he Car Preview node an d choose Clean and Build.

  This com piles t he files in t he proj ect so t hat you can u se t h e Car Pr eview class as a bean t hat can be dr agged an d dr opped dir ect ly on t o t he f or m .

  

Building a Java Desktop Database Application - NetBeans IDE Tutorial http://netbeans.org/kb/70/java/gui-db.html?print=yes

  This com pon ent was wr it t en as a Jav aBean s com ponent , so you could add it t o t he Palet t e, which wou ld be convenien t for addin g t he com ponen t t o m u lt iple app licat ions. Bu t f or n ow we w ill sim ply dr ag t h e com pon ent direct ly int o you r applicat ion dir ect ly f rom t he Pr oj ect s window.

  To a dd t h e Ca r Pr e v ie w com pon e nt t o t h e a p plica t ion : 1 . I n t he Pr oj ect s window, expand t he Car Preview > Sou rce Packages > car preview node.

  2 . Dr ag t he CarPreview.java class t o t h e for m . To in ser t it pr oper ly j u st below t he m enu s, place it over t he t able left aligned wit h t h e ot her cont r ols and snapped t o t he bot t om of t h e m en u bar, as sh ow n in t h e im age below .

  3 . Resize t he pr eview com pon ent hor izont ally over t h e en t ir e for m . 4 . I n t he sam e w ay t hat you bou nd t he slider s an d checkboxes t o elem en t s in t he masterTable com ponent , bind all t h e binding pr oper t ies of t h e CarPr eview com pon ent t o t he cor r esponding select edElem ent at t ribu t es of t h e masterTable . Use t he Bin d popup m enu or t he Bin din g t ab in t he pr opert y sheet . 5 . Ru n t he CarApp applicat ion again.

  I n t he ru nning applicat ion, you can see t he Car Pr eview com ponen t ch ange as you select differ ent r ows in t he t able, alt er valu es in t he t able, m ove t h e sliders, an d select and deselect t he ch eckboxes. The im age below show s t h e final r unn in g applicat ion.

  

Building a Java Desktop Database Application - NetBeans IDE Tutorial http://netbeans.org/kb/70/java/gui-db.html?print=yes

Building and Deploying t he Applicat ion

  Once you are sat isfied t h at you r applicat ion w or ks pr oper ly, you can prepar e t he applicat ion for deploym ent out side of t h e I DE. I n t his sect ion you will build t h e ap plicat ion 's dist r ibut able out put s an d r un t h e applicat ion fr om ou t side of t he I DE.

  Building t he Applicat ion

  The m ain bu ild com m and in t h e I DE is t he Clean and Build com m and. The Clean an d Build com m an d delet es pr eviously com piled classes and ot h er build ar t ifact s and t h en r ebu ilds t he ent ire proj ect fr om scr at ch.

  N ot e s: Ther e is also a Build com m and, wh ich does not delet e old bu ild ar t ifact s, but t his com m and is disabled by default .

  See t he Com pile on Save sect ion of t h e Creat in g, I m por t ing, and Con figur ing Java Pr oj ect s guide for m or e inf or m at ion.

  To bu ild t h e a pp lica t ion : Choose Run > Clean and Build Main Pr oj ect ( Shift - F11 ) .

  Out put f rom t he Ant build scr ipt appear s in t he Out put w in dow . I f t h e Ou t pu t window does not appear , you can op en it m anually by choosing Window > Ou t pu t > Ou t pu t . When you clean and bu ild your pr oj ect , t he following t hings occu r:

  Out put folder s t h at have been gener at ed by pr eviou s build act ions are delet ed ( " cleaned") . ( I n m ost cases, t h ese ar e t he

  build and dist folder s. )

  

Building a Java Desktop Database Application - NetBeans IDE Tutorial http://netbeans.org/kb/70/java/gui-db.html?print=yes

build and dist folder s are added t o you r pr oj ect folder ( h ereaft er r efer r ed t o as t he PROJECT_ HOME folder) .

  All of t he sour ces ar e com piled int o .class files, which ar e placed int o t he PROJECT_HOME /build folder. A JAR file con t aining your pr oj ect is cr eat ed inside t he PROJECT_HOME /dist folder . I f you have specif ied any libr ar ies for t he pr oj ect ( in addit ion t o t h e JDK) , a lib folder is cr eat ed in t h e dist folder . Th e libr ar ies ar e copied int o dist/lib .

  Th e m anifest file in t h e JAR is updat ed t o include ent ries t hat design at e t he m ain class an d an y libraries t hat ar e on t he proj ect 's classpat h .

  Running t he Applicat ion Out side of t he I DE To r u n t h e a pp lica t ion ou t sid e of t h e I D E:

  1 . St ar t Java DB fr om ou t side of t he I DE. See ht t p: / / db. apach e. or g/ derby/ docs/ dev/ get st ar t / N ot e : You can also st art Java DB fr om inside t he I DE, but t he server will be st opped w hen you exit t he I DE. 2 . Using your syst em 's f ile explor er or f ile m an ager , navigat e t o t he CarsApp/dist dir ect ory. 3 . Double- click t he CarsApp.jar file. Aft er a f ew seconds, t h e applicat ion sh ould st ar t .

  N ot e : I f double- clickin g t he JAR file does not lau nch t he applicat ion, see t his art icle for in for m at ion on set t in g JAR file associat ions in your oper at in g syst em .

  You can also laun ch t he applicat ion fr om t he com m and line.

  To la u n ch t h e a p plica t ion f r om t h e com m a n d lin e : 1 . St ar t Java DB fr om ou t side of t he I DE.

  2 . On your syst em , open up a com m an d pr om pt or t er m inal w indow . 3 . I n t h e com m and pr om pt , chan ge dir ect ories t o t he CarsApp/dist direct or y. 4 . At t he com m and line, t ype t h e following st at em ent :

  java -jar CarsApp.jar Dist ribut ing t he Applicat ion t o Ot her Users Now t hat you h ave verified t h at t he applicat ion works out side of t he I DE, you ar e ready t o dist r ibu t e t h e applicat ion .

  To dist r ibu t e t h e a pplica t ion :

  1 . On your syst em , creat e a zip f ile t h at con t ain s t h e applicat ion JAR file ( CarsApp.jar ) and t h e accom panying lib , wh ich cont ains t he ot her JAR f iles t hat t h e applicat ion needs. 2 . Send t he file t o t he peop le who will use t h e applicat ion. I nst ru ct t hem t o u npack t he zip file, m akin g sur e t hat t he CarsApp.jar file an d t he lib f older ar e in t he sam e folder . 3 . I nst r uct t h e user s t o f ollow t h e st eps in t he Run ning t he Applicat ion Ou t side of t he I DE sect ion above.

Next St eps This t ut or ial h as pr ovided an in t r oduct ion t o su ppor t for t he Sw in g Applicat ion Fram ewor k an d Bean s Binding in t h e I DE

  For inf or m at ion on u sing Hiber nat e f or a Swing applicat ion 's persist en ce layer , see Using Hiber nat e in a Java Swing Ap plicat ion . For a m ore infor m at ion on desig ning GUI applicat ions, see Design in g a Swing GUI .

  

Building a Java Desktop Database Application - NetBeans IDE Tutorial http://netbeans.org/kb/70/java/gui-db.html?print=yes

  For m or e inf or m at ion on u sing beans bin ding in t h e I DE, see Binding Beans and Dat a in a Deskt op Applicat ion .

  Send Feedback on Th is Tu t orial

See Also

  Gen eral Java Developm ent Learn ing Tr ail Net Bean s Plat form CRUD Applicat ion Tu t or ial GUI Edit or FAQ Java EE Tu t or ial: I nt r odu ct ion t o t he Java Persist ence API