Distributed Systems Course Material Home
Understanding Desktop Development
Desktop development is different from web development. In some ways it is easier and other ways harder. Let’s begin by looking at how we will had Desktop features to our application.
We are going to use Java Swing for our Desktop application. NetBeans has a drag-and-drop tool for it.
Creating Desktop Interface
Let us begin by creating a Java Swing screen that is similar to our web screen.
Next our screen needs to be able to communicate with the database.
Entity Classes
Next we want to generate Entity classes just like we did in our web application.
Now we need some code to have our screen talk with this database layer.
In the above example the EntityManager is used to create a new database record for Specimen using the persist method. Other important methods are:
- The EntityManager, in our case em, has a find method. You pass in the entity class type and the primary key. It returns the object. In our example it could be Specimen sp = em.find(Specimen.class, 1);
- The EntityManager also has a remove method. You can pass in the object to be removed, which will delete it from the database table.
- You can execute all of the NamedQuery’s that you see in the entity classes. They normally start around line 28. For example, you can do a find all. That code could look like this.
TypedQuery<Specimen> q = em.createNamedQuery(“Specimen.findAll”, Specimen.class);
java.util.List<Specimen> results = q.getResultList();
// now loop through the list called results. - If the NamedQuery that you would like to use has a parameter, like in the WHERE clause, you can use the setParameter method to set the value. In our example code above, object q, has the setParameter method.
Testing
If we did everything correctly, it should work.
If you would like to have your application open to a certain screen, here is how you do it.
Bonus Material: Java Look and Feel
If you do not like the look of the standard Java Swing components Java has a solution for that. A Java Look and Feel can replace the graphics for the GUI components. Many Java Looks and Feels are free to download and use.