Boosting JavaFX application development - No Fluff Just Stuff

Boosting JavaFX application development

Posted by: Andres Almiray on June 22, 2014

A good sign that JavaFX is finally taking off is the appearance of application frameworks. Although Griffon added support for JavaFX a couple of years ago (see javafx plugin) I'd like to bring your attention to what's the smallest JavaFX application framework so far: Afterburner.fx.

Afterburner.fx is the brain child of Adam Biem -- Java Champion, JavaOne Rockstar and well known advocate of Java EE. In Adam's own words, afterburner.fx is a minimalistic JavaFX MVP framework based on Convention over Configuration and Dependency Injection. This means you have the power of JSR 330 at your fingertips while not being underwhelmed of configuring every single aspect of the application.

Adam has made sure anyone can create a starting application using Maven archetypes, however I think that a much easier way to begin is to use Lazybones, which is another tool that can help you bootstrap any kind of project, including Afterburner.fx.

There are a couple of ways to get Lazybones installed onto your system; the easiest one has to be via GVM. Just execute the following command on a terminal prompt

$ curl -s get.gvmtool.net | bash

Follow the instructions on the screen and you're done. Now install Lazybones by invoking the following command

$ gvm install lazybones

That ought to do it. Lazybones performs its magic by downloading and executing project templates. The default repository (located at pledbrook/lazybones-templates on Bintray) contains w good number of starting templates. An additional step (just in case) is to configure a secondary template repository, by editing $USER_HOME/.lazybones/config.groovy

bintrayRepositories = [
      "aalmiray/kordamp",
      "pledbrook/lazybones-templates"
]

Listing all available templates is as easy as invoking

$ lazybones list
Available templates in aalmiray/kordamp:

    afterburnergfx
    afterburnerfx
    gradle-quickstart

Available templates in pledbrook/lazybones-templates:

    gradle-plugin
    nebula-plugin
    gaelyk
    gradle-quickstart
    ratpack
    groovy-lib
    ratpack-lite
    java-basic
    dropwizard
    groovy-app
    afterburnerfx
    lazybones-project
    spring-boot-actuator

We're now ready to create our first Afterburner.fx application.

$ lazybones create afterburner simple

A series of questions will follow, however there are some sensible defaults in case you don't want to type everything. Once the final answer is given you'll end up with a brand new project, whose structure looks like the following one

simple
+-- README.md
+-- build.gradle
+-- gradle
|   +-- javafx.gradle
|   \-- wrapper
|       +-- gradle-wrapper.jar
|       \-- gradle-wrapper.properties
+-- gradle.properties
+-- gradlew
+-- gradlew.bat
+-- pom.xml
\-- src
    \-- main
        +-- java
        |   \-- org
        |       \-- example
        |           +-- SimpleMain.java
        |           +-- SimplePresenter.java
        |           +-- SimpleService.java
        |           \-- SimpleView.java
        \-- resources
            \-- org
                \-- example
                    \-- simple.fxml

As you can appreciate the project can be built with both Maven or Gradle, giving you more options. I personally prefer the Gradle options as the configuration is much more shorter and conventional. The application is fully functional and can be executed with either

$ ./gradlew run

or using Maven

$ mvn compile jfx:run

From here on it's a matter of adding more components to the application, making sure to annotate injection points with @javax.inject.Inject.

Now, some people love XML and some don't. For those that find themselves in the latter category there's an alternative to dealing with FXML: GroovyFX. Do you remember the days of old where JavaFX applications had to be written using a language named JavaFX Script? Well that language is long gone now but you can still write UIs using a DSL in the form of Groovy. This is precisely what GroovyFX is. Bertrand Goetzman has taken Adam's framework and added a bit of GroovyFX spice to it, resulting in Afterburner.gfx (mind the G in the name). If you look back to the list of templates available at aalmiray/kordamp you'll see there's an option for this framework too; this means you can create an afterburner.gfx project using Lazybones.

$ lazybones create afterburnergfx simple

This results in the following project structure

simple
+-- README.md
+-- build.gradle
+-- gradle
|   +-- javafx.gradle
|   \-- wrapper
|       +-- gradle-wrapper.jar
|       \-- gradle-wrapper.properties
+-- gradle.properties
+-- gradlew
+-- gradlew.bat
\-- src
    \-- main
        +-- groovy
        |   \-- org
        |       \-- example
        |           +-- SimpleMain.java
        |           +-- SimplePresenter.java
        |           +-- SimpleService.java
        |           \-- SimpleView.java
        \-- resources
            \-- org
                \-- example
                    \-- simple.groovyfx

Very similar to the first application only that this time there's no Maven build file and the default view uses GroovyFX instead of FXML. The Groovy language is very friendly with the Java language, this is why you can mix them together in the same project, which is exactly what what's happening here. The application can be run like the previous one too

$ ./gradlew run

Be sure to keep an eye on Adam and Bertrand, as they keep improving their respective frameworks in order to make it dead simple to organize and build JavaFX applications.

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 »