Geertjan is a DZone Zone Leader and has posted 460 posts at DZone. You can read more from them at their website. View Full User Profile

How to Create a Swing CRUD Application on NetBeans Platform 6.8

12.08.2009
| 141661 views |
  • submit to reddit

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

The application you create in this article will look as follows:

the final state of the application

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 (described here), 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.

  1. Choose File > New Project (Ctrl+Shift+N). Under Categories, select NetBeans Modules. Under Projects, select NetBeans Platform Application. Click Next.

  2. 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.

NetBeans Platform container

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.

  1. For purposes of this example, use the Services window to connect to the sample database that is included with NetBeans IDE:

    Services window

    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.

  2. In the IDE, choose File | New Project, followed by Java | Java Class Library to create a new library project named CustomerLibrary.

  3. 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.

    adding tables

  4. Specify the persistence strategy, which can be any of the available options. Here, since we need to choose something, we'll choose EclipseLink:

    choosing eclipselink

  5. Specify "demo" as the name of the package where the entity classes will be generated.

    name of package

  6. 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:

    entity classes

  7. 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:

    dist folder

Published at DZone with permission of its author, Geertjan Wielenga.

Comments

Steve Sdas replied on Fri, 2012/10/12 - 3:42pm

New Project, followed by Java | Java Class Library to create a new library project named CustomerLibrary. italian male singers

Steve Sdas replied on Fri, 2012/10/12 - 3:46pm in response to: Geertjan Wielenga

You guys make it really easy for all the folks out there. italian male singers

Steve Sdas replied on Mon, 2012/11/12 - 3:29pm

This will definitely be very useful for me when I get a chance to start my blog. http://waterbayec.org

Uilly Tocky replied on Mon, 2012/12/31 - 5:28am

The many angles trained on any particular character discourage identification and ensure, if not alienation, at least a dispassionate distance. ppi claims calculator

Uilly Tocky replied on Sat, 2013/02/02 - 6:44am

This is the type of info that should be shared around the net. Shame on Google for no longer positioning this publish higher!nursery school program

Uilly Tocky replied on Mon, 2013/02/04 - 4:50am

I am wondering how I might be notified whenever a new post has been made.Dermatend Review

Uilly Tocky replied on Tue, 2013/02/05 - 11:23am

Your writers are enormously tremendous. glass display cabinet

Uilly Tocky replied on Wed, 2013/02/06 - 9:01am

I need more articles and blogs please post soon. reverse phone lookup

Uilly Tocky replied on Fri, 2013/02/08 - 6:43am

I'm hoping to give a contribution & help other customers like its aided me. Great job.Can-C

Roger Sall replied on Tue, 2013/04/09 - 1:29pm

Create Swing Crud is in the backlinks and blogeral. At have much scripts

Roxanne Mioto replied on Fri, 2013/04/12 - 1:17pm

It is this flexibility that the modular architecture of the NetBeans Platform makes possible melatoninaácido ursólico , lipo 6 ultra

Uilly Tocky replied on Sun, 2013/04/14 - 10:27am

It feels awesome to read such informative and unique articles on your websites.Ganhar Dinheiro Winner Manager

Junio Mousull replied on Wed, 2013/04/17 - 8:40pm

I would take this opportunity to emphasize the importance of 1. Define roles and tasks (I prefer "roles and responsibilities"), and bbom

Junio Mousull replied on Wed, 2013/04/24 - 9:35am

NetBeans users could learn a lot about Spring RCP by reading the steps in this dhea 100mg

Junio Mousull replied on Sat, 2013/05/04 - 7:01pm

When we have a module for our editor, we begin adding CRUD functionality. First, the "R", standing for "Read", bbom 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".

Junio Mousull replied on Tue, 2013/05/07 - 1:04pm

There have been a few important junctures in my life, and when they’ve happened, I’ve always had unexpected (but welcome) company.

By nite burn

Uilly Tocky replied on Sun, 2013/05/12 - 5:24am

I'm gladsome to mature so more serviceable and informative assemblage on your website. Stop Smoking Today - No Pills to swallow or Gum to Chew

Uilly Tocky replied on Fri, 2013/05/24 - 1:11am

Thanks for sharing such a useful information, I will be checking your blog for further updates and information. Philadelphia Realtor

Uilly Tocky replied on Sun, 2013/05/26 - 11:49am

I guess I haven’t read such unique material anywhere else online. buy raspberry ketone

Uilly Tocky replied on Sun, 2013/05/26 - 12:16pm

These articles and blogs are genuinely sufficiency for me for a day. How to treat a fungus nail

Uilly Tocky replied on Mon, 2013/05/27 - 7:38am

I wana thank you for providing instructive and qualitative stuff so often.Does Acai Berry Really Work

Uilly Tocky replied on Fri, 2013/05/31 - 5:20am

I daily read your blogs and give my notice for that here this article is too great so interestingbreast actives reviews

Danish12 Ali replied on Mon, 2013/07/08 - 5:56am

 But in its new decision, FWS reiterated its earlier conclusions, saying the Sonoran Desert population doesn't qualify as a "distinct population segment" because it is not different enough from other bald eagle populations. haynes t-shirt

Danish12 Ali replied on Sat, 2013/07/13 - 5:42am

 Very nice post. I just stumbled upon your blog and wanted to say that I've really enjoyed browsing your blog posts. After all I will be subscribing to your feed and I hope you write again soon  cialis

Henkz Sall replied on Mon, 2013/10/14 - 5:39pm in response to: Craig Ringer

While that keeps it simple, it also makes it a videos

Muhammad Danial replied on Mon, 2014/03/31 - 9:56am

 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. 

Freezer repair

Samad Khatri replied on Thu, 2014/06/26 - 10:27am in response to: Craig Ringer

 Java DB was chosen for this article because it is easiest to get started with, since it comes with the JDK.

ideearredobagno.it

Comment viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.