Griffon: JavaFX for the Enterprise. - No Fluff Just Stuff

Griffon: JavaFX for the Enterprise.

Posted by: Andres Almiray on July 3, 2012

A few weeks ago I presented a talk on JavaFX and Griffon in Linz hosted by eJUG. A couple of Griffon (Swing and JavaFX) applications were shown on stage, with two of them going toe to toe in showing how easy is to integrate JPA into a desktop application no matter the UI toolkit of choice. I'm going to reproduce both applications right here in order to show the similarities and differences between choosing Swing or JavaFX when building an application with Griffon. Let's get started.

Create the applications
The first step is to create both applications. Swing being the default toolkit of choice at the moment makes it for a simple command call, like the following one

$ griffon create-app sample1

Good, now creating the JavaFX application requires specifying an additional parameter that marks the application as a JavaFX one

$ griffon create-app sample2 --archetype=javafx

That wasn't so bad, we know have two fully working Griffon applications although they do nothing more than pop a window with a simple message when run. Let's change that.

Configuring JPA
The following step must be executed in both applications. What we'll do is install the JPA plugin, create a JPA entity, configure the default persistence unit, and bootstrap some data when the application starts.
Alright, installing the JAP is done in the following way

$ griffon install-plugin jpa

Done. Next create a file named Person.groovy inside src/main/data; you may need to create that directory first.

This is a standard JPA entity class, the Groovy language makes it easy to keep track of what's important by removing most of the visual cruft and verbosity :-D. Now that we have this entity class is time to define the configuration for the default persistence unit the applications will make use of, this task is accomplished by creating a file persistence.xml inside griffon-app/conf/metainf

The applications make use of H2 and Eclipselink, dependencies that must be configured in griffon-app/conf/BuildConfig.groovy in a block that looks like this one

The last task in this step is to define some data that will be used to fill the database. A pair of files were added to griffon-app/conf when the JPA plugin was installed; one of these files is aptly named BootstrapJpa.groovy, which is exactly where we'll place the following code

Alright, these should be enough to take care of the JPA stuff. Time to move on to the next challenge: building the UI.

Adding Swing / JavaFX
The following screenshots show the Swing and JavaFX versions. you can appreciate both have the same set of elements though they look slightly different. Can you guess which is the JavaFX one? That's right, the one with the non-native look (pay no heed to the title)

So how does the UI of the first application is constructed? Griffon relies on a SwingBuilder DSL to make it easier to write the UI. With this DSL in place is easy to distinguish the component hierarchy and how elements are connected
griffon-app/views/sample1/Sample1View.groovy

The JavaFX view relies on a similar trick, that is, make use of a DSL however in this case it's based on SceneGraphBuilder (from the GroovyFX project). See if you can spot the real differences between both views
griffon-app/views/sample2/Sample2View.groovy

I must confess that binding model.people directly to the tableView's items property was done as a desperate measure as I couldn't get the tableView to be updated automatically whenever model.people (a JavaFX ObservableList) had its contents updated. Oh well, something to review when the next JavaFX release comes out (currently testing with JavaFX 2.2beta15 on OSX and JDK6 for those who care).

The views have references to their corresponding models and controllers. The models hold a reference to an observable list that will be used to update the table. In the case of Swing we'll rely on the excellent GlazedLists project (for which there's a plugin of course). Here's how the model looks
griffon-app/models/sample1/Sample1Model.groovy

For the JavaFX version the code turns out to be much simpler as observable lists are baked right into the JavaFX APIs. The JavaFX model looks like this
griffon-app/models/sample2/Sample2Model.groovy

Now for the final piece, we must provide a suitable implementation for the controller actions. You may recall from the views that there are 2 actions in the controller: one to load the data in the table, the second to clear the table. And here's where things get really interesting as the code I'm about to show is practically the same, no matter if you're dealing with Swing or JavaFX
griffon-app/controllers/sample1/Sample1Controller.groovy

Don't be fooled by the apparent lack of UI code found in the controller actions, as Griffon controllers follow some rules regarding how threading should work. For starters all controller actions are executed outside of the UI thread (whether that's the EDT in Swing or the JavaFX UI thread in JavaFX only the specific toolkit plugin cares). It's then safe to query the database using the JPA APIs inside the load action, however once the data has been computed we must update the model inside the UI thread as that change will trigger an update on the visual components bound to the people list (the JTable and the TableView respectively). The second action, clear, updates the contents of the model directly, and as such it should be done in the UI thread already, no need to jump to another thread just to come back immediately to the UI thread; this is why we applied an annotation that instructs the compiler to leave this action untouched in terms of thread management code injection.

If the usage of an application framework like Griffon was dubious at first I hope you think differently now. Apart from some toolkit specifics found in the View and Model, the application remains the same no matter the toolkit you're targeting. This proves to be of great advantage for developers willing to give JavaFX a try, as starting with Swing today doesn't mean rewriting the whole application with JavaFX later. Compound the growing list of plugins available to Griffon applications and you're up for a fun ride when developing desktop applications for the JVM.

In terms of numbers both applications are very close. These are the stats for the Swing version

Whereas the JavaFX version sports these numbers

Not bad, huh? Full source code for both applications can be obtained from this link.

Andres Almiray

About Andres Almiray

Andres is a Java/Groovy developer and a Java Champion with more than 20 years of experience in software design and development. He has been involved in web and desktop application development since the early days of Java. Andres is a true believer in open source and has participated on popular projects like Groovy, Griffon, and DbUnit, as well as starting his own projects (Json-lib, EZMorph, GraphicsBuilder, JideBuilder). Founding member of the Griffon framework and Hackergarten community event. https://ch.linkedin.com/in/aalmiray

Why Attend the NFJS Tour?

  • » Cutting-Edge Technologies
  • » Agile Practices
  • » Peer Exchange

Current Topics:

  • Languages on the JVM: Scala, Groovy, Clojure
  • Enterprise Java
  • Core Java, Java 8
  • Agility
  • Testing: Geb, Spock, Easyb
  • REST
  • NoSQL: MongoDB, Cassandra
  • Hadoop
  • Spring 4
  • Cloud
  • Automation Tools: Gradle, Git, Jenkins, Sonar
  • HTML5, CSS3, AngularJS, jQuery, Usability
  • Mobile Apps - iPhone and Android
  • More...
Learn More »