JavaFX Script in a GlassFish app client - featuring persistence this time
By tjquinn on Nov 09, 2007
This example implements a very simple address book. And as with my earlier JavaFX script examples, I am sure the U/I itself could be done much better and my JavaFX script style probably could be better. Even so, the sample shows what I wanted it to: that we can use a local persistence unit in an app client to retrieve and store information for display and modification using JavaFX script.
The application allows the user to manage contacts, each of which can have multiple addresses of the same or different types (e-mail, home phone, office phone, mobile). The app client uses the default GlassFish database.
Download the NetBeans project JAR and expand it into a directory and open it from NetBeans. You will need to resolve the server reference to the GlassFish server you have defined to NetBeans.
Some interesting things to notice (interesting to me, anyway):
- There are three entity classes which use the persistence annotations to define the relationships among them.
- As with the earlier example, the main Java class in the app client provides some convenience methods that the JavaFX script uses. This lets the app client container handle the persistence annotations automatically.
- This JavaFX script U/I relies heavily on binding between the U/I
elements and data objects. Bindings do not work well with Java objects
(there are some firstname.lastname@example.org
mailing list postings about why). So I've created JavaFX objects that
shadow the persistence-annotated Java objects. As part of retrieving the persistent Java objects using the main class convenience methods, the JavaFX script builds shadow objects each of which refers to
its Java object counterpart. As part of saving the user's changes to the data, the JavaFX script copies the current attribute values from the a JavaFX shadow object to its corresponding Java persistent object before asking the main Java class to persist the Java objects.
The JavaFX script U/I declarations bind to the attributes on the JavaFX shadow classes - which works fine. I did have to add the extra scripting to copy the values back and forth between the shadow JavaFX classes and the Java objects, but it wasn't too bad. Ideally this would not be necessary or would be handled by a developer or graphic designer tool.
Even in the absence of tool support for this, there might be a better way to deal with this, but this worked fairly quickly for my purposes so I went with it.
- Even though there is no real server component to this example, I build an EAR and put the app client inside it. This allows me to package the JavaFX JARs in the EAR, which GlassFish makes available to the app client. Otherwise I would have to put copies of the JavaFX JARs into the extension directory for the JRE on the client system (or make them available to the client in some other way). This way, GlassFish takes care of everything at the cost of a bit larger EAR and app client JAR.
If you want to try this example, download the NetBeans project above and the JavaFX JARs. Unjar the NetBeans project on your system, then add your downloaded JavaFX JARs to the JavaFXJars directory in the unjarred NetBeans project.
Make sure you have started the Derby database using the asadmin start-database command. Then deploy the EAR. To run the client, you can either:
- Run the EAR (not the app client subproject) from NetBeans.
- Retrieve the app client JAR using the --retrieve option on the deploy
command. Then use the appclient command to launch the client.
- Launch the app client using Java Web Start.
You may see warnings about tables or rows already existing in the database if you redeploy the app. This should not harm the functioning of the app.