How to Create a Swing CRUD Application on NetBeans Platform 6.8
this article shows you how to integrate a java db database into a netbeans platform application. we start by exploring a java db database, from which we create entity classes. next, we wrap the entity classes into a module, together with modules for the related jpa jars. note: these instructions are not applicable to java db only. rather, they are relevant to any relational database, such as oracle or mysql. several applications on the netbeans platform, many of which are listed here , use these databases too. java db was chosen for this article because it is easiest to get started with, since it comes with the jdk. once the above modules are part of our application, we create a new module that provides the user interface for our application. the new module gives the user a tree hierarchy showing data from the database. we then create another module that lets the user edit the data displayed by the first module. by separating the viewer from the editor in distinct modules, we will enable the user to install a different editor for the same viewer, since different editors could be created by external vendors, some commercially and some for free. it is this flexibility that the modular architecture of the netbeans platform makes possible. when we have a module for our editor, we begin adding crud functionality. first, the "r", standing for "read", is handled by the viewer described above. next, the "u" for "update" is handled, followed by the "c" for "create", and the "d" for "delete". at the end of the article, you will have learned about a range of netbeans platform features that help you in creating applications of this kind. for example, you will have learned about the undoredo.manager and the explorermanager , as well as netbeans platform swing components, such as topcomponent and beantreeview . contents setting up the application integrating the database creating entity classes from a database wrapping the entity class jar in a module creating other related modules designing the user interface setting dependencies running the prototype integrating crud functionality read update create delete the application you create in this article will look as follows: source code: http://kenai.com/projects/nbcustomermanager once you're at the stage shown above, you can simply download a netbeans module that provides office laf support ( ), add it to your application, and then when you redeploy the application, you will see this: note: it is advisable to watch the screencast series top 10 netbeans apis before beginning to work on this article. many of the concepts addressed in this article are discussed in more detail within the screencast series. setting up the application let's start by creating a new netbeans platform application. choose file > new project (ctrl+shift+n). under categories, select netbeans modules. under projects, select netbeans platform application. click next. in the name and location panel, type dbmanager in the project name field. click finish. the ide creates the dbmanager project. the project is a container for all the other modules you will create. run the application and notice that you have quite a few features out of the box already. open some of the windows, undock them, and get to know the basic components that the netbeans platform provides without you doing any work whatsoever: integrating the database in order to integrate the database, you need to create entity classes from your database and integrate those entity classes, together with their related jars, into modules that are part of your netbeans platform application. creating the entity classes in this section, you generate entity classes from a selected database. for purposes of this example, use the services window to connect to the sample database that is included with netbeans ide: note: alternatively, use any database you like and adapt the steps that follow to your particular use case. in the case of mysql, see connecting to a mysql database . in the ide, choose file | new project, followed by java | java class library to create a new library project named customerlibrary. in the projects window, right-click the library project and choose file | new file, followed by persistence | entity classes from database. in the wizard, select your database and the tables you need. here we choose "customer", and then "discount code" is added automatically, since there is a relationship between these two tables. specify the persistence strategy, which can be any of the available options. here, since we need to choose something, we'll choose eclipselink: specify "demo" as the name of the package where the entity classes will be generated. click finish. once you have completed this step, look at the generated code and notice that, among other things, you now have a persistence.xml file in a folder called meta-inf, as well as entity classes for each of your tables: build the java library and you will have a jar file in the library project's "dist" folder, which you can view in the files window: wrapping the entity class jar in a module in this section, you add your first module to your application! the new netbeans module will wrap the jar file you created in the previous section. right-click the dbmanager's modules node in the projects window and choose add new library. select the jar you created in the previous subsection and complete the wizard, specifying any values you like. let's assume the application is for dealing with customers at shop.org, in which case a unique identifier "org.shop.model" is appropriate for the code name base: you now have your first custom module in your new application, wrapping the jar containing the entity classes and the persistence.xml file: creating other related modules in this section, you create two new modules, wrapping the eclipselink jars, as well as the database connector jar. do the same as you did when creating the library wrapper for the entity class jar, but this time for the eclipselink jars, which are in the "customerlibrary" java library that you created earlier: note: in the library wrapper module wizard, you can use ctrl-click to select multiple jars. next, create yet another library wrapper module, this time for the java db client jar, which is available in your jdk distribution, at db/lib/derbyclient.jar. designing the user interface in this section, you create a simple prototype user interface, providing a window that uses a jtextarea to display data retrieved from the database. right-click the dbmanager's modules node in the projects window and choose add new. create a new module named customerviewer, with the code name base org.shop.ui. in the projects window, right-click the new module and choose new | window component. specify that it should be created in the editor position and that it should open when the application starts. set customer as the window's class name prefix. use the palette (ctrl-shift-8) to drag and drop a jtextarea on the new window: add this to the end of the topcomponent constructor: entitymanager entitymanager = persistence.createentitymanagerfactory("customerlibrarypu").createentitymanager(); query query = entitymanager.createquery("select c from customer c"); list resultlist = query.getresultlist(); for (customer c : resultlist) { jtextarea1.append(c.getname() + " (" + c.getcity() + ")" + "\n"); } note: since you have not set dependencies on the modules that provide the customer object and the persistence jars, the statements above will be marked with red error underlines. these will be fixed in the section that follows. above, you can see references to a persistence unit named "customerlibrarypu", which is the name set in the persistence.xml file. in addition,there is a reference to one of the entity classes, called customer, which is in the entity classes module. adapt these bits to your needs, if they are different to the above. setting dependencies in this section, you enable some of the modules to use code from some of the other modules. you do this very explicitly by setting intentional contracts between related modules, i.e., as opposed to the accidental and chaotic reuse of code that tends to happen when you do not have a strict modular architecture such as that provided by the netbeans platform. the entity classes module needs to have dependencies on the derby client module as well as on the eclipselink module. right-click the customerlibrary module, choose properties, and use the libraries tab to set dependencies on the two modules that the customerlibrary module needs. the customerviewer module needs a dependency on the eclipselink module as well as on the entity classes module. right-click the customerviewer module, choose properties, and use the libraries tab to set dependencies on the two modules that the customerviewer module needs. open the customertopcomponent in the source view, right-click in the editor, and choose "fix imports". the ide is now able to add the required import statements, because the modules that provide the required classes are now available to the customertopcomponent. you now have set contracts between the modules in your application, giving you control over the dependencies between distinct pieces of code. running the prototype in this section, you run the application so that you can see that you're correctly accessing your database. start your database server. run the application. you should see this: you now have a simple prototype, consisting of a netbeans platform application that displays data from your database, which you will extend in the next section. integrating crud functionality in order to create crud functionality that integrates smoothly with the netbeans platform, some very specific netbeans platform coding patterns need to be implemented. the sections that follow describe these patterns in detail. read in this section, you change the jtextarea, introduced in the previous section, for a netbeans platform explorer view. netbeans platform explorer views are swing components that integrate better with the netbeans platform than standard swing components do. among other things, they support the notion of a context, which enables them to be context sensitive. representing your data, you will have a generic hierarchical model provided by a netbeans platform node class, which can be displayed by any of the netbeans platform explorer views. this section ends with an explanation of how to synchronize your explorer view with the netbeans platform properties window. in your topcomponent, delete the jtextarea in the design view and comment out its related code in the source view: entitymanager entitymanager = persistence.createentitymanagerfactory("customerlibrarypu").createentitymanager(); query query = entitymanager.createquery("select c from customer c"); list resultlist = query.getresultlist(); //for (customer c : resultlist) { // jtextarea1.append(c.getname() + " (" + c.getcity() + ")" + "\n"); //} right-click the customerviewer module, choose properties, and use the libraries tab to set dependencies on the nodes api and the explorer & property sheet api. next, change the class signature to implement explorermanager.provider: final class customertopcomponent extends topcomponent implements explorermanager.provider you will need to override getexplorermanager() @override public explorermanager getexplorermanager() { return em; } at the top of the class, declare and initialize the explorermanager: private static explorermanager em = new explorermanager(); note: watch top 10 netbeans apis for details on the above code, especially the screencast dealing with the nodes api and the explorer & property sheet api. switch to the topcomponent design view, right-click in the palette, choose palette manager | add from jar. then browse to the org-openide-explorer.jar, which is in platform11/modules folder, within the netbeans ide installation directory. choose the beantreeview and complete the wizard. you should now see beantreeview in the palette. drag it from the palette and drop it on the window. create a factory class that will create a new beannode for each customer in your database: import demo.customer; import java.beans.introspectionexception; import java.util.list; import org.openide.nodes.beannode; import org.openide.nodes.childfactory; import org.openide.nodes.node; import org.openide.util.exceptions; public class customerchildfactory extends childfactory { private list resultlist; public customerchildfactory(list resultlist) { this.resultlist = resultlist; } @override protected boolean createkeys(list list) { for (customer customer : resultlist) { list.add(customer); } return true; } @override protected node createnodeforkey(customer c) { try { return new beannode(c); } catch (introspectionexception ex) { exceptions.printstacktrace(ex); return null; } } } back in the customertopcomponent, use the explorermanager to pass the result list from the jpa query in to the node: entitymanager entitymanager = persistence.createentitymanagerfactory("customerlibrarypu").createentitymanager(); query query = entitymanager.createquery("select c from customer c"); list resultlist = query.getresultlist(); em.setrootcontext(new abstractnode(children.create(new customerchildfactory(resultlist), true))); //for (customer c : resultlist) { // jtextarea1.append(c.getname() + " (" + c.getcity() + ")" + "\n"); //} run the application. once the application is running, open the properties window. notice that even though the data is available, displayed in a beantreeview, the beantreeview is not synchronized with the properties window, which is available via window | properties. in other words, nothing is displayed in the properties window when you move up and down the tree hierarchy. synchronize the properties window with the beantreeview by adding the following to the constructor in the topcomponent: associatelookup(explorerutils.createlookup(em, getactionmap())); here we add the topcomponent's actionmap and explorermanager to the lookup of the topcomponent. run the application again and notice that the properties window is now synchronized with the explorer view: now you are able to view your data in a tree hierarchy, as you would be able to do with a jtree. however, you're also able to swap in a different explorer view without needing to change the model at all because the explorermanager mediates between the model and the view. finally, you are now also able to synchronize the view with the properties window. update in this section, you first create an editor. the editor will be provided by a new netbeans module. so, you will first create a new module. then, within that new module, you will create a new topcomponent, containing two jtextfields, for each of the columns you want to let the user edit. you will need to let the viewer module communicate with the editor module. whenever a new node is selected in the viewer module, you will add the current customer object to the lookup. in the editor module, you will listen to the lookup for the introduction of customer objects. whenever a new customer object is introduced into the lookup, you will update the jtextfields in the editor. next, you will synchronize your jtextfields with the netbeans platform's undo, redo, and save functionality. in other words, when the user makes changes to a jtextfield, you want the netbeans platform's existing functionality to become available so that, instead of needing to create new functionality, you'll simply be able to hook into the netbeans platform's support. to this end, you will need to use the undoredomanager, together with the savecookie. create a new module, named customereditor, with org.shop.editor as its code name base. right-click the customereditor module and choose new | window component. make sure to specify that the window should appear in the editor position and that it should open when the application starts. in the final panel of the wizard, set "editor" as the class name prefix. use the palette (ctrl-shift-8) to add two jlabels and two jtextfields to the new window. set the texts of the labels to "name" and "city" and set the variable names of the two jtextfields to jtextfield1 and jtextfield2. in the gui builder, the window should now look something like this: go back to the customerviewer module and change its layer.xml file to specify that the customertopcomponent window will appear in the explorer mode. note: right-click the application project and choose "clean", after changing the layer.xml file. why? because whenever you run the application and close it down, the window positions are stored in the user directory. therefore, if the customerviewer was initially displayed in the editor mode, it will remain in the editor mode, until you do a "clean", thus resetting the user directory (i.e., thus deleting the user directory) and enabling the customerviewer to be displayed in the position currently set in the layer.xml file. also check that the beantreeview in the customerviewer will stretch horizontally and vertically when the user resizes the application. check this by opening the window, selecting the beantreeview, and then clicking the arrow buttons in the toolbar of the gui builder. run the application and make sure that you see the following when the application starts up: now we can start adding some code. firstly, we need to show the currently selected customer object in the editor: start by tweaking the customerviewer module so that the current customer object is added to the viewer window's lookup whenever a new node is selected. do this by creating an abstractnode, instead of a beannode, in the customerchildfactory class. that enables you to add the current customer object to the lookup of the node, as follows (note the "lookups.singleton(c)" below): @override protected node createnodeforkey(customer c) { node node = new abstractnode(children.leaf, lookups.singleton(c)); node.setdisplayname(c.getname()); node.setshortdescription(c.getcity()); return node; // try { // return new beannode(c); // } catch (introspectionexception ex) { // exceptions.printstacktrace(ex); // return null; // } } now, whenever a new node is created, which happens when the user selects a new customer in the viewer, a new customer object is added to the lookup of the node. let's now change the editor module in such a way that its window will end up listening for customer objects being added to the lookup. first, set a dependency in the editor module on the module that provides the entity class, as well as the module that provides the persistence jars. next, change the editortopcomponent class signature to implement lookuplistener: public final class editortopcomponent extends topcomponent implements lookuplistener override the resultchanged so that the jtextfields are updated whenever a new customer object is introduced into the lookup: @override public void resultchanged(lookupevent lookupevent) { lookup.result r = (lookup.result) lookupevent.getsource(); collection coll = r.allinstances(); if (!coll.isempty()) { for (customer cust : coll) { jtextfield1.settext(cust.getname()); jtextfield2.settext(cust.getcity()); } } else { jtextfield1.settext("[no name]"); jtextfield2.settext("[no city]"); } } now that the lookuplistener is defined, we need to add it to something. here, we add it to the lookup.result obtained from the global context. the global context proxies the context of the selected node. for example, if "ford motor co" is selected in the tree hierarchy, the customer object for "ford motor co" is added to the lookup of the node which, because it is the currently selected node, means that the customer object for "ford motor co" is now available in the global context. that is what is then passed to the resultchanged, causing the text fields to be populated. all of the above starts happening, i.e., the lookuplistener becomes active, whenever the editor window is opened, as you can see below: @override public void componentopened() { result = utilities.actionsglobalcontext().lookupresult(customer.class); result.addlookuplistener(this); resultchanged(new lookupevent(result)); } @override public void componentclosed() { result.removelookuplistener(this); result = null; } since the editor window is opened when the application starts, the lookuplistener is available at the time that the application starts up. finally, declare the result variable at the top of the class, like this: private lookup.result result = null; run the application again and notice that the editor window is updated whenever you select a new node: however, notice what happens when you switch the focus to the editor window: because the node is no longer current, the customer object is no longer in the global context. this is the case because, as pointed out above, the global context proxies the lookup of the current node. therefore, in this case, we cannot use the global context. instead, we will use the local lookup provided by the customer window. rewrite this line: result = utilities.actionsglobalcontext().lookupresult(customer.class); to this: result = windowmanager.getdefault().findtopcomponent("customertopcomponent").getlookup().lookupresult(customer.class); the string "customertopcomponent" is the id of the customertopcomponent, which is a string constant that you can find in the source code of the customertopcomponent. one drawback of the approach above is that now our editortopcomponent only works if it can find a topcomponent with the id "customertopcomponent". either this needs to be explicitly documented, so that developers of alternative editors can know that they need to identify the viewer topcomponent this way, or you need to rewrite the selection model, as described here by tim boudreau. if you take one of the above approaches, you will find that the context is not lost when you switch the focus to the editortopcomponent, as shown below: note: since you are now using abstractnode, instead of beannode, no properties are shown in the properties window. you need to provide these yourself, as described in the nodes api tutorial . secondly, let's work on the undo/redo functionality. what we'd like to have happen is that whenever the user makes a change to one of the jtextfields, the "undo" button and the "redo" button, as well as the related menu items in the edit menu, become enabled. to that end, the netbeans platform makes the undoredo.manager available. declare and instantiate a new undoredomanager at the top of the editortopcomponent: private undoredo.manager manager = new undoredo.manager(); next, override the getundoredo() method in the editortopcomponent: @override public undoredo getundoredo() { return manager; } in the constructor of the editortopcomponent, add a keylistener to the jtextfields and, within the related methods that you need to implement, add the undoredolisteners: jtextfield1.getdocument().addundoableeditlistener(manager); jtextfield2.getdocument().addundoableeditlistener(manager); run the application and show the undo and redo functionality in action, the buttons as well as the menu items. the functionality works exactly as you would expect. you might want to change the keylistener so that not all keys cause the undo/redo functionality to be enabled. for example, when enter is pressed, you probably do not want the undo/redo functionality to become available. therefore, tweak the code above to suit your business requirements. thirdly, we need to integrate with the netbeans platform's save functionality: by default, the "save all" button is available in the netbeans platform toolbar. in our current scenario, we do not want to save "all", because "all" refers to a number of different documents. in our case, we only have one "document", which is the editor that we are reusing for all the nodes in the tree hirerarchy. remove the "save all" button and add the "save" button instead, by adding the following to the layer file of the customereditor module: when you now run the application, you will see a different icon in the toolbar. instead of the "save all" button, you now have the "save" button available. set dependencies on the dialogs api and the nodes api. in the editortopcompontn constructor, add a call to fire a method (which will be defined in the next step) whenever a change is detected: public editortopcomponent() { ... ... ... jtextfield1.getdocument().adddocumentlistener(new documentlistener() { public void insertupdate(documentevent arg0) { fire(true); } public void removeupdate(documentevent arg0) { fire(true); } public void changedupdate(documentevent arg0) { fire(true); } }); jtextfield2.getdocument().adddocumentlistener(new documentlistener() { public void insertupdate(documentevent arg0) { fire(true); } public void removeupdate(documentevent arg0) { fire(true); } public void changedupdate(documentevent arg0) { fire(true); } }); //create a new instance of our savecookie implementation: impl = new savecookieimpl(); //create a new instance of our dynamic object: content = new instancecontent(); //add the dynamic object to the topcomponent lookup: associatelookup(new abstractlookup(content)); } ... ... ... here are the two methods referred to above. first, the method that is fired whenever a change is detected. an implementation of the savecookie from the nodes api is added to the instancecontent whenever a change is detected: public void fire(boolean modified) { if (modified) { //if the text is modified, //we add savecookie impl to lookup: content.add(impl); } else { //otherwise, we remove the savecookie impl from the lookup: content.remove(impl); } } private class savecookieimpl implements savecookie { @override public void save() throws ioexception { confirmation message = new notifydescriptor.confirmation("do you want to save \"" + jtextfield1.gettext() + " (" + jtextfield2.gettext() + ")\"?", notifydescriptor.ok_cancel_option, notifydescriptor.question_message); object result = dialogdisplayer.getdefault().notify(message); //when user clicks "yes", indicating they really want to save, //we need to disable the save action, //so that it will only be usable when the next change is made //to the jtextarea: if (notifydescriptor.yes_option.equals(result)) { fire(false); //implement your save functionality here. } } } run the application and notice the enablement/disablement of the save button: note: right now, nothing happens when you click ok in the dialog above. in the next step, we add some jpa code for handling persistence of our changes. next, we add jpa code for persisting our change. do so by replacing the comment "//implement your save functionality here." the comment should be replaced by all of the following: entitymanager entitymanager = persistence.createentitymanagerfactory("customerlibrarypu").createentitymanager(); entitymanager.gettransaction().begin(); customer c = entitymanager.find(customer.class, customer.getcustomerid()); c.setname(jtextfield1.gettext()); c.setcity(jtextfield2.gettext()); entitymanager.gettransaction().commit(); note: the "customer" in customer.getcustomerid() is currently undefined. add the line "customer = cust;" in the resultchanged (as shown below), after declaring customer customer; at the top of the class, so that the current customer object sets the customer, which is then used in the persistence code above to obtain the id of the current customer object. @override public void resultchanged(lookupevent lookupevent) { lookup.result r = (lookup.result) lookupevent.getsource(); collection c = r.allinstances(); if (!c.isempty()) { for (customer customer : c) { customer = cust; jtextfield1.settext(customer.getname()); jtextfield2.settext(customer.getcity()); } } else { jtextfield1.settext("[no name]"); jtextfield2.settext("[no city]"); } } run the application and change some data. currently, we have no "refresh" functionality (that will be added in the next step) so, to see the changed data, restart the application. here, for example, the tree hierarchy shows the persisted customer name for "toyota motor co": fourthly, we need to add functionality for refreshing the customer viewer. you might want to add a timer which periodically refreshes the viewer. however, in this example, we will add a "refresh" menu item to the root node so that the user will be able to manually refresh the viewer. in the main package of the customerviewer module, create a new node, which will replace the abstractnode that we are currently using as the root of the children in the viewer. note that we also bind a "refresh" action to our new root node. public class customerrootnode extends abstractnode { public customerrootnode(children kids) { super(kids); setdisplayname("root"); } @override public action[] getactions(boolean context) { action[] result = new action[]{ new refreshaction()}; return result; } private final class refreshaction extends abstractaction { public refreshaction() { putvalue(action.name, "refresh"); } public void actionperformed(actionevent e) { customertopcomponent.refreshnode(); } } } add this method to the customertopcomponent, for refreshing the view: public static void refreshnode() { entitymanager entitymanager = persistence.createentitymanagerfactory("customerlibrarypu").createentitymanager(); query query = entitymanager.createquery("select c from customer c"); list resultlist = query.getresultlist(); em.setrootcontext(new customerrootnode(children.create(new customerchildfactory(resultlist), true))); } now replace the code above in the constructor of the customertopcomponent with a call to the above. as you can see, we are now using our customerrootnode instead of the abstractnode. the customerrootnode includes the "refresh" action, which calls the code above. in your save functionality, add the call to the method above so that, whenever data is saved, an automatic refresh takes place. you can take different approaches when implementing this extension to the save functionality. for example, you might want to create a new module that contains the refresh action. that module would then be shared between the viewer module and the editor module, providing functionality that is common to both. run the application again and notice that you have a new root node, with a "refresh" action: make a change to some data, save it, invoke the refresh action, and notice that the viewer is updated. you have now learned how to let the netbeans platform handle changes to the jtextfields. whenever the text changes, the netbeans platform undo and redo buttons are enabled or disabled. also, the save button is enabled and disabled correctly, letting the user save changed data back to the database. create in this section, you allow the user to create a new entry in the database. right-click the customereditor module and choose "new action". use the new action wizard to create a new "always enabled" action. the new action should be displayed anywhere in the toolbar and/or anywhere in the menu bar. in the next step of the wizard, call the action newaction. note: make sure that you have a 16x16 icon available, which the wizard forces you to select if you indicate that you want the action to be invoked from the toolbar. in the new action, let the topcomponent be opened, together with emptied jtextfields: import java.awt.event.actionevent; import java.awt.event.actionlistener; public final class newaction implements actionlistener { public void actionperformed(actionevent e) { editortopcomponent tc = editortopcomponent.getdefault(); tc.resetfields(); tc.open(); tc.requestactive(); } } note: the action implements the actionlistener class, which is bound to the application via entries in the layer file, put there by the new action wizard. imagine how easy it will be when you port your existing swing application to the netbeans platform, since you'll simply be able to use the same action classes that you used in your original application, without needing to rewrite them to conform to action classes provided by the netbeans platform! in the editortopcomponent, add the following method for resetting the jtextfields and creating a new customer object: public void resetfields() { customer = new customer(); jtextfield1.settext(""); jtextfield2.settext(""); } in the savecookie, ensure that a return of null indicates that a new entry is saved, instead of an existing entry being updated: public void save() throws ioexception { confirmation message = new notifydescriptor.confirmation("do you want to save \"" + jtextfield1.gettext() + " (" + jtextfield2.gettext() + ")\"?", notifydescriptor.ok_cancel_option, notifydescriptor.question_message); object result = dialogdisplayer.getdefault().notify(msg); //when user clicks "yes", indicating they really want to save, //we need to disable the save button and save menu item, //so that it will only be usable when the next change is made //to the text field: if (notifydescriptor.yes_option.equals(result)) { fire(false); entitymanager entitymanager = persistence.createentitymanagerfactory("customerlibrarypu").createentitymanager(); entitymanager.gettransaction().begin(); if (customer.getcustomerid() != null) { customer c = entitymanager.find(customer.class, cude.getcustomerid()); c.setname(jtextfield1.gettext()); c.setcity(jtextfield2.gettext()); entitymanager.gettransaction().commit(); } else { query query = entitymanager.createquery("select c from customer c"); list resultlist = query.getresultlist(); customer.setcustomerid(resultlist.size()+1); customer.setname(jtextfield1.gettext()); customer.setcity(jtextfield2.gettext()); //add more fields that will populate all the other columns in the table! entitymanager.persist(customer); entitymanager.gettransaction().commit(); } } } run the application again and add a new customer to the database. delete in this section, let the user delete a selected entry in the database. using the concepts and code outlined above, implement the delete action yourself. create a new action, deleteaction. decide whether you want to bind it to a customer node or whether you'd rather bind it to the toolbar, the menu bar, keyboard shortcut, or combinations of these. depending on where you want to bind it, you will need to use a different approach in your code. read the article again for help, especially by looking at how the "new" action was created, while comparing it to the "refresh" action on the root node. get the current customer object, return an 'are you sure?' dialog, and then delete the entry. for help on this point, read the article again, focusing on the part where the "save" functionality is implemented. instead of saving, you now want to delete an entry from the database. see also this concludes the article. you have learned how to create a new netbeans platform application with crud functionality for a given database. you have also seen many of the netbeans apis in action. for more information about creating and developing applications on the netbeans platform, see the following resources: netbeans platform learning trail netbeans api javadoc
December 8, 2009
·
231,024 Views
Comments
Dec 27, 2014 · Priyanka Sharma
Many more tutorials on NetBeans plugins here:
https://netbeans.org/features/platform/all-docs.html
May 20, 2013 · Geertjan Wielenga
Great article, many thanks for writing it, indeed in the context of maps and globes, this is a great solution.
Jan 07, 2012 · Toni Epple
Nov 23, 2010 · Geertjan Wielenga
Nov 23, 2010 · Geertjan Wielenga
Nov 23, 2010 · Geertjan Wielenga
Sep 11, 2010 · Geertjan Wielenga
symbol : variable WindowManager
location: class org.sqleditor.branding.Installer
WindowManager.getDefault().inkoveWhenUIReady(
3 errors
From the above, you can see that you need "WindowManager" class. That's part of the Window System API. Add that module as a dependency to your module: go to the Project Properties dialog of the module and add "Window System API" as one of the APIs that the module depends on. Apologies, I should have specified that in the instructions above!
Sep 07, 2010 · Geertjan Wielenga
Sep 07, 2010 · Geertjan Wielenga
Sep 07, 2010 · Geertjan Wielenga
Sep 07, 2010 · Geertjan Wielenga
Sep 07, 2010 · Geertjan Wielenga
Sep 07, 2010 · Geertjan Wielenga
Sep 07, 2010 · Geertjan Wielenga
Sep 07, 2010 · Geertjan Wielenga
Apr 01, 2010 · Geertjan Wielenga
Mar 27, 2010 · Geertjan Wielenga
Feb 19, 2010 · Geertjan Wielenga
Dec 30, 2009 · Christopher Lam
Download link works fine for me:
http://netbeans.dzone.com/sites/all/files/CustomerApp_PrimeFaces.zip
Dec 14, 2009 · Geertjan Wielenga
Hi surajchchetry, that's a great idea. If I understand it correctly. This is the kind of project structure I am creating at the moment, for a new tutorial, based on your suggestion above.
A new tutorial should appear within the next week or two, to explain how to create project structures like the above, with a bunch of additional features too, as described here in my blog!
Dec 14, 2009 · Geertjan Wielenga
Dec 13, 2009 · Geertjan Wielenga
Dec 09, 2009 · Geertjan Wielenga
Excellent feedback, ringerc. That will be fixed. Thank you. One approach is to register actions in the layer file which then enables you to add an attribute to the layer entry. That attribute causes the action to be processed asynchronously, (which is a new feature in 6.8, i.e., the ability to mark an action as being asynchronous). These asynchronous actions can simulate SwingWorker:
Another approach would be to use SwingWorker. These approaches will be added to the article, though note that threading on the NetBeans Platform is also discussed here: http://wiki.netbeans.org/NetBeansDeveloperFAQ#Threading
Something else that's possible is to integrate with the NetBeans progress bar, which can be achieved with, literally, 3 lines of code.
Dec 02, 2009 · Geertjan Wielenga
Great feedback. Maybe a refcard on modulerization should be created. The basic principles listed above are great starting points. It's also cool to see that various module systems have the same basic rationales underpinning them.
Also, based on the above various comments, could one perhaps say: "when in doubt about whether or not to create another module, go ahead and do so"? I.e., the fact that you're thinking about it is probably a reason to go ahead and follow that impulse, since it is probably appropriate in your context.
Mar 05, 2009 · Geertjan Wielenga
Mar 05, 2009 · Geertjan Wielenga
Feb 14, 2009 · Geertjan Wielenga
Jan 17, 2009 · Geertjan Wielenga
I only find a drawback: Whenever I need to make changes to entity library (which is a regular NB IDE Java Library Project), I am then also required to manually copy the new generated .jar file into the NB platform library wrapper module project (step nr. 2 in the post). In fact, I need to copy it directly to the project directory, having to have a knowledge on its filesystem layout.
This makes it not really agile if the above happens often (which, BTW, is my case).
[/quote]
Workaround: create a new Ant target and include it in your build process. During the build, the JAR will be created afresh in the NetBeans module, automatically, without you needing to do anything special (once you've created that Ant target). (See "Updating the Sources" here.)
Jan 17, 2009 · Geertjan Wielenga
I only find a drawback: Whenever I need to make changes to entity library (which is a regular NB IDE Java Library Project), I am then also required to manually copy the new generated .jar file into the NB platform library wrapper module project (step nr. 2 in the post). In fact, I need to copy it directly to the project directory, having to have a knowledge on its filesystem layout.
This makes it not really agile if the above happens often (which, BTW, is my case).
[/quote]
Workaround: create a new Ant target and include it in your build process. During the build, the JAR will be created afresh in the NetBeans module, automatically, without you needing to do anything special (once you've created that Ant target). (See "Updating the Sources" here.)
Jan 17, 2009 · Geertjan Wielenga
I'm also considering filling in a bug report on this, as I do not really happen to see any justification for this fact. Having solved that, one could keep developing a Suite (or NBP-based app.) which does include a "JPA-enabled" module all within the great NB IDE and also let it keep track of all dependencies.
[/quote]
+1 on that! Here's my issue on this, please go in there and agree with me:
http://www.netbeans.org/issues/show_bug.cgi?id=155214
Thanks for liking my humor, nice to hear. :-)
Jan 17, 2009 · Geertjan Wielenga
I'm also considering filling in a bug report on this, as I do not really happen to see any justification for this fact. Having solved that, one could keep developing a Suite (or NBP-based app.) which does include a "JPA-enabled" module all within the great NB IDE and also let it keep track of all dependencies.
[/quote]
+1 on that! Here's my issue on this, please go in there and agree with me:
http://www.netbeans.org/issues/show_bug.cgi?id=155214
Thanks for liking my humor, nice to hear. :-)
Dec 10, 2008 · Geertjan Wielenga
Dec 10, 2008 · Geertjan Wielenga
Nov 30, 2008 · Geertjan Wielenga
Jul 02, 2008 · Jordi R Cardona
Here are some other articles on this topic that might be found useful:
http://www.netbeans.org/community/magazine/html/03/schliemann/
http://www.netbeans.org/community/magazine/html/04/schliemann.html
http://platform.netbeans.org/tutorials/60/nbm-prolog.html
Apr 17, 2008 · Geertjan Wielenga
@Shay: wow that stuff in JDeveloper looks cool. I think I might take JDeveloper for a spin soon especially for that reason.
@shaulgo: you're right, without the sources debugging would be problematic. However, I really like the idea of being able to look at the internals of a Swing app where I would simply have the JAR and nothing else. Just for the fun of it, but I'm guessing there'd be practical reaons too.
@Maxim, congrats with this cool tool and hope to catch up with you at JavaOne!
Apr 16, 2008 · Geertjan Wielenga
Apr 10, 2008 · Geertjan Wielenga
Apr 08, 2008 · Geertjan Wielenga
This whole thing is such a bad idea on so many levels. If you have a good UI design, why do you want to move stuff around?
[/quote]
As pointed out by one or two people above, maybe you have different end users and the person doing the packaging is not a Java programmer. The Spring config file would be a way for the assembler, a non Java programmer, to assemble an application from the decoupled Swing components provided by the programmer.
[quote=cm38280]
How do people maintaining your code figure out where some panel class MyPanel exists within your application?
[/quote]
Most IDEs (at least NetBeans does, for sure) have support within Spring config files exactly for that purpose. You can click on a reference to a Java source file and then the source file in question opens. You also have code completion for the value of the "class" attribute, so that can be used for filling in the values and verifying that the referenced class actually exists.
[quote=cm38280]
Spring is fantastic at configuring "service" classes which can be accessed from within an app but it is not an appropriate XML description on so many levels for configuring instances of any old POJO (whether that be data classes or GUI components).
[/quote]
I agree that this approach is probably atypical. But at the same time there could be scenarios out there where this could be useful. And the XML would not necessarily be reams and reams of tags either -- possibly you'd have three or four panels that could be exchanged with each other and the assembler would do this via the Spring config file. Again, I'm not making this scenario up myself -- look at the comments above where at least one respondent sees this as being applicable to their real life needs.
[quote=cm38280]
I see "bean, I see "property", I see "value" and I see "list" but nowhere do I get a feel for what the panel contains nor its layout.
[/quote]
In the scenario of there being an assembler of the application, that's not relevant, they don't need to see the panel (or other decoupled Swing component) in question. However, if they do want to see the layout or other content, they can simply click to it from the Spring config file (as pointed out above).
[quote=cm38280]
Just take one step back and look at the sheer awfulness of the XML you've posted above!
[/quote]
I think I've responded to these points. I'm not arguing that this is useful for each and every Swing app, I'm just pointing out that this is possible in those cases where it makes sense.
Apr 08, 2008 · Geertjan Wielenga
This whole thing is such a bad idea on so many levels. If you have a good UI design, why do you want to move stuff around?
[/quote]
As pointed out by one or two people above, maybe you have different end users and the person doing the packaging is not a Java programmer. The Spring config file would be a way for the assembler, a non Java programmer, to assemble an application from the decoupled Swing components provided by the programmer.
[quote=cm38280]
How do people maintaining your code figure out where some panel class MyPanel exists within your application?
[/quote]
Most IDEs (at least NetBeans does, for sure) have support within Spring config files exactly for that purpose. You can click on a reference to a Java source file and then the source file in question opens. You also have code completion for the value of the "class" attribute, so that can be used for filling in the values and verifying that the referenced class actually exists.
[quote=cm38280]
Spring is fantastic at configuring "service" classes which can be accessed from within an app but it is not an appropriate XML description on so many levels for configuring instances of any old POJO (whether that be data classes or GUI components).
[/quote]
I agree that this approach is probably atypical. But at the same time there could be scenarios out there where this could be useful. And the XML would not necessarily be reams and reams of tags either -- possibly you'd have three or four panels that could be exchanged with each other and the assembler would do this via the Spring config file. Again, I'm not making this scenario up myself -- look at the comments above where at least one respondent sees this as being applicable to their real life needs.
[quote=cm38280]
I see "bean, I see "property", I see "value" and I see "list" but nowhere do I get a feel for what the panel contains nor its layout.
[/quote]
In the scenario of there being an assembler of the application, that's not relevant, they don't need to see the panel (or other decoupled Swing component) in question. However, if they do want to see the layout or other content, they can simply click to it from the Spring config file (as pointed out above).
[quote=cm38280]
Just take one step back and look at the sheer awfulness of the XML you've posted above!
[/quote]
I think I've responded to these points. I'm not arguing that this is useful for each and every Swing app, I'm just pointing out that this is possible in those cases where it makes sense.
Apr 08, 2008 · Geertjan Wielenga
This whole thing is such a bad idea on so many levels. If you have a good UI design, why do you want to move stuff around?
[/quote]
As pointed out by one or two people above, maybe you have different end users and the person doing the packaging is not a Java programmer. The Spring config file would be a way for the assembler, a non Java programmer, to assemble an application from the decoupled Swing components provided by the programmer.
[quote=cm38280]
How do people maintaining your code figure out where some panel class MyPanel exists within your application?
[/quote]
Most IDEs (at least NetBeans does, for sure) have support within Spring config files exactly for that purpose. You can click on a reference to a Java source file and then the source file in question opens. You also have code completion for the value of the "class" attribute, so that can be used for filling in the values and verifying that the referenced class actually exists.
[quote=cm38280]
Spring is fantastic at configuring "service" classes which can be accessed from within an app but it is not an appropriate XML description on so many levels for configuring instances of any old POJO (whether that be data classes or GUI components).
[/quote]
I agree that this approach is probably atypical. But at the same time there could be scenarios out there where this could be useful. And the XML would not necessarily be reams and reams of tags either -- possibly you'd have three or four panels that could be exchanged with each other and the assembler would do this via the Spring config file. Again, I'm not making this scenario up myself -- look at the comments above where at least one respondent sees this as being applicable to their real life needs.
[quote=cm38280]
I see "bean, I see "property", I see "value" and I see "list" but nowhere do I get a feel for what the panel contains nor its layout.
[/quote]
In the scenario of there being an assembler of the application, that's not relevant, they don't need to see the panel (or other decoupled Swing component) in question. However, if they do want to see the layout or other content, they can simply click to it from the Spring config file (as pointed out above).
[quote=cm38280]
Just take one step back and look at the sheer awfulness of the XML you've posted above!
[/quote]
I think I've responded to these points. I'm not arguing that this is useful for each and every Swing app, I'm just pointing out that this is possible in those cases where it makes sense.
Apr 07, 2008 · Geertjan Wielenga
Apr 07, 2008 · Geertjan Wielenga
Making sure that your components are decoupled and easy to test is all very well, but you don't need to use Spring to achieve that. You could just as easily use all of the decoupled classes that you have written but wire them together in some Java code.
I fail to see the advantage of 100s of lines of XML, several JAR dependencies and moving errors from compile time to runtime, over a much more concise Java wiring of the components.
[/quote]
I think there might be several reasons why one would take this approach. A simple usecase might be if the developer/s is/are already familiar with Spring in the context of the web/Java EE world. In that context, it would be nice to continue using the same framework within the desktop space.
Secondly, imagine that, instead of 4 or 5 Swing components as in this example, you had dozens, or even hundreds or thousands. Having one centralized location where you manage the interactions between these components can only be a good thing, right? Because in that case your wiring code would be spread across MANY Java classes, I don't believe having a single Java class handling all the interactions of thousands of Swing components would be conceivable.
Apr 07, 2008 · Geertjan Wielenga
Making sure that your components are decoupled and easy to test is all very well, but you don't need to use Spring to achieve that. You could just as easily use all of the decoupled classes that you have written but wire them together in some Java code.
I fail to see the advantage of 100s of lines of XML, several JAR dependencies and moving errors from compile time to runtime, over a much more concise Java wiring of the components.
[/quote]
I think there might be several reasons why one would take this approach. A simple usecase might be if the developer/s is/are already familiar with Spring in the context of the web/Java EE world. In that context, it would be nice to continue using the same framework within the desktop space.
Secondly, imagine that, instead of 4 or 5 Swing components as in this example, you had dozens, or even hundreds or thousands. Having one centralized location where you manage the interactions between these components can only be a good thing, right? Because in that case your wiring code would be spread across MANY Java classes, I don't believe having a single Java class handling all the interactions of thousands of Swing components would be conceivable.
Apr 07, 2008 · Geertjan Wielenga
Making sure that your components are decoupled and easy to test is all very well, but you don't need to use Spring to achieve that. You could just as easily use all of the decoupled classes that you have written but wire them together in some Java code.
I fail to see the advantage of 100s of lines of XML, several JAR dependencies and moving errors from compile time to runtime, over a much more concise Java wiring of the components.
[/quote]
I think there might be several reasons why one would take this approach. A simple usecase might be if the developer/s is/are already familiar with Spring in the context of the web/Java EE world. In that context, it would be nice to continue using the same framework within the desktop space.
Secondly, imagine that, instead of 4 or 5 Swing components as in this example, you had dozens, or even hundreds or thousands. Having one centralized location where you manage the interactions between these components can only be a good thing, right? Because in that case your wiring code would be spread across MANY Java classes, I don't believe having a single Java class handling all the interactions of thousands of Swing components would be conceivable.
Apr 07, 2008 · Geertjan Wielenga
Making sure that your components are decoupled and easy to test is all very well, but you don't need to use Spring to achieve that. You could just as easily use all of the decoupled classes that you have written but wire them together in some Java code.
I fail to see the advantage of 100s of lines of XML, several JAR dependencies and moving errors from compile time to runtime, over a much more concise Java wiring of the components.
[/quote]
I think there might be several reasons why one would take this approach. A simple usecase might be if the developer/s is/are already familiar with Spring in the context of the web/Java EE world. In that context, it would be nice to continue using the same framework within the desktop space.
Secondly, imagine that, instead of 4 or 5 Swing components as in this example, you had dozens, or even hundreds or thousands. Having one centralized location where you manage the interactions between these components can only be a good thing, right? Because in that case your wiring code would be spread across MANY Java classes, I don't believe having a single Java class handling all the interactions of thousands of Swing components would be conceivable.
Apr 02, 2008 · Geertjan Wielenga
Thanks. :-)
Part 2 was published sometime ago already:
http://java.dzone.com/news/how-add-resize-functionality-v
Mar 04, 2008 · Geertjan Wielenga
Feb 22, 2008 · Geertjan Wielenga
Feb 21, 2008 · Geertjan Wielenga
Feb 21, 2008 · Geertjan Wielenga
Here are some tips for Hermien:
http://www.techienuggets.com/Comments?tx=2752
Feb 21, 2008 · Geertjan Wielenga
Feb 19, 2008 · Geertjan Wielenga
Feb 06, 2008 · Geertjan Wielenga
Jacek, I agree with you that not everything must be in Java. However, on the other hand, I find these arguments from the Lobo page pretty convincing in this regard, under the heading "Why a Pure Java Browser?":
There are a number of advantages to be derived from a browser that is written in Java as opposed to a language compiled into native code, namely:Feb 06, 2008 · Geertjan Wielenga
Jacek, I agree with you that not everything must be in Java. However, on the other hand, I find these arguments from the Lobo page pretty convincing in this regard, under the heading "Why a Pure Java Browser?":
There are a number of advantages to be derived from a browser that is written in Java as opposed to a language compiled into native code, namely:Feb 06, 2008 · Geertjan Wielenga
Jacek, I agree with you that not everything must be in Java. However, on the other hand, I find these arguments from the Lobo page pretty convincing in this regard, under the heading "Why a Pure Java Browser?":
There are a number of advantages to be derived from a browser that is written in Java as opposed to a language compiled into native code, namely:Feb 06, 2008 · Geertjan Wielenga
Jacek, I agree with you that not everything must be in Java. However, on the other hand, I find these arguments from the Lobo page pretty convincing in this regard, under the heading "Why a Pure Java Browser?":
There are a number of advantages to be derived from a browser that is written in Java as opposed to a language compiled into native code, namely: