/ DataFX / DataFX Tutorial 6

DataFX Tutorial 6

Hendrik on 2015/01/22 - 09:05 in DataFX, JavaFX

In this tutorial I want to show how dynamic actions can be handled in DataFX. In all the last tutorials the actions are defined in the flow configuration or by using annotations. By doing so you can’t dynamically choose which action should be called when clicking a button for example. To do so DataFX provides the FlowActionHandler class. This class provides methods to call DataFX actions directly in code. Here are some examples:

As you can see the FlowActionHandler class provides methods for all the action types that are supported in DataFX by using annotations or the flow configuration.
Normally you want to use the FlowActionHandler inside of a view to call dynamic actions based on a user interaction. To do so DataFX provides the @ActionHandler annotation that can be used to inject the FlowActionHandler in a view controller. Here is a small example how this will look:

As you can see it is really easy to call DataFX actions in code. As a next step we want to create a small application that uses this features.
Let’s say we have a main view and 3 sub views. In the main view you can choose by using radio buttons to which view you want to navigate.
To do so we create a controller with the “navigate” buttons and the radio buttons. As a next step we need to define a ToggleGroup for all the radio buttons. This group manages the RadioButton instances. By doing so you can only select one RadioButton. This is a JavaFX basic feature. If you want to now more about RadioButtons and ToggleGroups you should buy this book or visit this training. After this is done the controller should look like this:

As you can see in the code I don’t use the @FXML annotation. With DataFX 8 we introduced the @ViewNode annotation to inject components of the view in the controller. Basically this annotation can be used as the @FXML annotation but it provides more features. You can define the fx:id of the node as the value of the annotation, for example. By doing so your field must not have the same name as the node in the FXML file. Here is a short example how this can be used:

In addition the JSR-377 is currently planned and this will introduce a UI Toolkit independent annotation to inject view components. DataFX will implement the JSR once it is released and therefor we started to define our own basic annotations. SO maybe the name of the annotation will change once the JSR is released.
Once the controller is finished we need to add the action handler and navigate to the selected view. The RadioButtons instances in the controller define the controller classes for all the subviews as user data. By doing so we can simply navigate to the selected user data. Here is the final code for the controller:

Once this is done you can select the wanted view in the UI and by clicking the “navigate” button the view will change to the selected one.
All the view controllers for the subviews are defined as shown in earlier tutorials. You can find the complete code of the example here.


Send Us A Message Here

Your email address will not be published. Required fields are marked *

  • 2015/01/26

    Hello, datafx is a great light weight(for now) for javafx.
    Good design, like the flow idea. about how it control all the controller and view is going.

    I just get confused with getting this tutorial running. I checked the maven repos. and seems the latest version is 8.0 ? I just sorted it by date on the http://mvnrepository.com/ , then figure it might be the latest version.

    But 8.0 is still with @FXMLController and @ViewController is not available.

    I know it’s a lot of work to do. but would u please update the blog or index of website, show the latest version. so we can catch on. or should I follow the source from https://bitbucket.org/datafx/datafx/, and build it myself?

    Thank you for your hard and excellent work .

    • 2015/01/26


      I merged the new annotation names last week. They will be part of DataFX 8.1 and we will release it the next weeks.

  • 2015/02/03

    I’m one of the many people who don’t use Maven.
    I do find that the code resources are not exclusively Maven.
    That’s good.

    I let Eclipse, Netbeans, and Scene Builder do their thing.
    Then I can spend my time on my research goals and development,
    rather than get buried in this weeks latest tools.
    That, for example, is why I don’t use JUnit.

    I’m a one-person operation, so among other things,
    I don’t have to merge other people’s code versions – no other people!

    I find Git, via SourceTree, a handy way to keep track of
    versions, with useful commit comments.
    But I almost never go back to previous versions.
    I’m always moving forwards.

    That’s my take on all this.

    JavaFX is great. Such a relief after those terrible Swing years.

    I’m a retired CS professor – Still busy after 55 years of programming.
    Throw in ten or fifteen years each of focus on physics, then biology.

    Punched cards, then onwards and upwards.

    These days, 100% focus on NLP for biology research articles.

    – Bob

    Bob Futrelle
  • 2015/10/13


    Thanks for these tutorials.

    Can you explain in the next App tutorial how i can navigate between screens passing parameters?

    I’m creating an application where i need for example in View1 search some people into database, show result in TableView and have possibility to edit one selected people into View2, for that i need load View2 passing the id of people selected in View1.

    Thanks a lot.

  • 2015/12/28

    I am using datafx-flow 8.0 but i can’t find @ViewNode annotation. As you mentioned you have merged these annotations in DataFX 8.1, but how to add it in pom. I can’t find it in maven central. latest one on maven central is 8.0.7.