The Griffon Trove: dialogs and JavaFX - No Fluff Just Stuff

The Griffon Trove: dialogs and JavaFX

Posted by: Andres Almiray on July 9, 2012

Avid JavaFX developers may have noticed that the JavaFX 2.0 APIs provide Window and Stage classes that may be roughly mapped to java.awt.Window and javax.swing.Frame. However there's no counterpart class for building dialogs. If you search for examples you'll find that the most commonly suggested solution is to configure an Stage instance. Here's how to do it with Griffon and a temporary MVC group.

The first step is to create a new MVC group, let's call it dialog for simplicity's sake (though you can pick *any* name you want).

$ griffon create-mvc dialog

This command creates the following files
  • griffon-app/models/sample/DialogModel.groovy
  • griffon-app/views/sample/DialogView.groovy
  • griffon-app/controllers/sample/DialogController.groovy
In may case the default package chosen is sample as that's the name of the application I created at an earlier stage with the following command

$ griffon create-app sample --archetype=javafx

Alright. Now that we have a starting point for the new MVC group let's change each member in order, starting with the Model. The Model holds basic information that will be used to tweak the dialog's behavior and its looks

The Model defines two observable properties that can be used to specify a title and a textual content for the dialog. It also sets the default modality to true, which means the dialog will block its owner window. Next we move to the View where we can appreciate the model properties in action

Two things of important note about this piece of code
  1. the Model has JavaFX properties given that some properties were annotated with @FXBindable. These properties can be used with standard JavaFX bindings.
  2. Apparently you can't set alignment nor margins on the center component of a borderPane node. This is caused by a name clash between builder variables and borderPane pseudo nodes (bottom is not a real node, but a method call handled by the borderPane factory, hence "pseudo node"). What we see at the end of the View script is a temporary workaround by making direct usage of the JavaFX APIs.
Next is the Controller and third piece of this group. The Controller's responsibility is to assemble the Stage and configure it given the information found in the Model and View.

We see two actions in this controller: one to show the dialog, the second to hide it. Both actions are annotated with @Threading(Threading.Policy.INSIDE_UITHREAD_SYNC) as they are only concerned with UI stuff, otherwise they would get executed outside of the UI thread and we'll get an ugly exception from the JavaFX platform. Notice how the show action configures a freshly created Stage instance. Now all that's left is make use of this dialog; the following controller shows both model and non-modal usages

In the modal action we make use of withMVCGroup. This method instantiates a new MVC group with the specified parameters taking special care of releasing any resources when the group is no longer needed, which is the case when the dialog is dismissed. Given that the dialog is modal the call to withMVCGroup waits until the dialog is closed. We see a different method used on the other action as the dialog is non-modal. createMVCGroup is the most common way to build an MVC group instance. Notice that this action sets the modality to false. Also, the name of the group instance must be calculated explicitly, as there's the potential of multiple dialogs of the same type being visible at any given time.

Keep on Groovying!

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 »