1. Bertrand Goetzmann
  2. afterburner.gfx


Clone wiki

afterburner.gfx / Home


Welcome to afterburner.gfx project!

This project can be seen as an extension to the excellent Adam Bien's JavaFX MVP framework afterburner.fx: it adds the possibility to use GroovyFX views in afterburner.fx projects. So, this is why you find a g character just after the dot in the project's name.

Here the main aspects on this project:

  • It uses Gradle (and JavaFX Gradle plugin) as build tool.
  • Having afterburner.fx 1.5.0 as dependency, afterburner.gfx supplies GroovyFXView class, that uses GroovyFX's SceneGraphBuilder to build JavaFX views, like in the Griffon framework.
  • Through the static method GroovyFXView.build(), you can dynamically build JavaFX views using the powerful GroovyFX's DSL.

I created the followme.gfx project as demonstration of the use of this project.

You will find afterburner.gfx binary available on Bintray.

A great tool that will help you to start a new afterburner.gfx project is Lazybones; for more information about it, read Boosting JavaFX application development.

Using views in afterburner.gfx

As in the original afternurner.fx project, a view can be defined by a FXML file, but you also define a view with a Groovy .groovyfx file that contains code accepted by SceneGraphBuildder in order to build JavaFX nodes.

A FXML file view like this one:

<?xml version="1.0" encoding="UTF-8"?>

<?import java.lang.*?>
<?import java.net.*?>
<?import java.util.*?>
<?import javafx.scene.*?>
<?import javafx.scene.control.*?>
<?import javafx.scene.input.*?>
<?import javafx.scene.layout.*?>

<AnchorPane id="AnchorPane" minHeight="180.0" prefHeight="192.0" prefWidth="525.0" styleClass="airpad" xmlns:fx="http://javafx.com/fxml" fx:controller="com.airhacks.followme.presentation.followme.FollowmePresenter">
    <Label fx:id="message" layoutX="141.0" layoutY="51.0" prefHeight="39.0" prefWidth="347.0" text="A simplest possible afterburner.fx application" />
    <Button fx:id="launch" layoutX="141.0" layoutY="134.0" mnemonicParsing="false" onAction="#launch" text="Launch" />

could be rewritten like this, in afterburner.gfx (thanks to GroovyFX):

anchorPane(id: 'anchorPane', minHeight: 180.0, prefHeight: 192.0, prefWidth: 525.0, styleClass: 'airpad') {
    label(id: 'message', layoutX: 141.0, layoutY: 51.0, prefHeight: 39.0, prefWidth: 347.0, 'A simplest possible afterburner.gfx application')
    button(layoutX: 230.0, layoutY: 134.0, mnemonicParsing: false, onAction: { presenter.launch() }, 'Launch')

Note that the GroovyFX view, as in this previous example, must return a javafx.scene.Parent. You can even do something like this:

def g = group {
    // ...
// some code here

So, if you want to use a FXML view definition, you have to extend FXMLView class and put your .fxml in the same package as your new view class; and for a GroovyFX view, you need to extend GroovyFXView class with the .groovyfx file in the same package as your view class.

How the presenter class is found

When using a FXML view file, the presenter class is specified by the fx:controller XML attribute and, normally, it should be a full qualified class name that is in the same package as the view class. In addition, the class name should be the view name (without the extension part) fowllowed by Presenter. So, it's a convention to follow.

For a GroovyFX view, it's enforced: afterburner.gfx will try to instantiate a class that follows the convention indicated.

In a Groovy view, you can also use the presenter instance linked to it through the implicit variable presenter.

Binding variables

There is a way, in a presenter class, to get JavaFX node instances injected in attributes class by using the @FXML annotation: for each XML node holding a fx:id, the framework will try to inject the corresponding JavaFX node instance into a attribute presenter class that has the same name of the id and marked by the @FXML annotation.

For a GroovyFX view, the id attribute on a GroovyFX node plays the same role as the fx:id attribute.

Building dynamic views

With the static method GroovyFXView.build(), you can dynamically build JavaFX views; here an example of a presenter class that builds a view in a method:

class BuilderPresenter {   

    def anchorPane

    // This method gives an example of use of GroovyFXView.build static method
    void buildView() {
        def child = GroovyFXView.build {
            button("Click me!", onAction: { println 'Hello, World!' })