DataFX Controller API

The [DataFX]({{ site.baseurl }}{% link pages/projects/datafx.md %}) team will show some cool new APIs at JavaOne this year. Johan has written a post about writeback support in the new DataFX version some days ago. Today I will give a short preview to another part of the new DataFX APIs.

The Controller API

The base of the API is the inversion of control for JavaFX controllers. The API offers the annotation FXMLController that can be assigned to any controller class. By using this annotation you can define the FXML-View that should be used by this controller. If you don‘t define the name of the FXML-File the API will try to find the view by using a convention over configuration approach. By doing so you do not need to add Java class information in your FXML file. You can use all default FXML annotations that are defined by JavaFX. By doing so you can inject all your nodes that are defined in the fxml-file in the controller by simply adding the @FXML annotation. Additionally you can use the @PostConstruct annotation. A simple Controller that is created by this API can look like this one:

@FXMLController("Details.fxml")
public class DetailViewController {
        @FXML
        private TextField myTextfield;
        @FXML
        private Button backButton;
        @PostConstruct
        public void init() {
            myTextfield.setText("Hello!");
        }
}

To load the view and the controller DataFX-Controller offers a simple HelperClass called ViewFactory. This is the default entry point to the complete DataFX-Controller API. You can create a view and controller by using this class:

ViewFactory.getInstance().createByController(DetailViewController.class);

The FXML-file that is used by this controller can look like this one:

<?xml version="1.0" encoding="UTF-8"?>
<?import javafx.scene.control.*?>
<?import javafx.scene.layout.*?>
<hbox spacing="10" alignment="bottom_right" xmlns:fx="http://javafx.com/fxml">
    <textfield fx:id="myTextfield" prefwidth="90" />
    <button fx:id="backButton" text="back">
</button></hbox>

If you want to see more code check out the DataFX 2.0 repository at bitbucket.

We will provide more samples and tutorials later, and you are very welcome to join us at JavaOne (CON3202, Wednesday Sep 25, 08:30 am, Hilton - Plaza B).

Hendrik Ebbers

Hendrik Ebbers is the founder of Open Elements. He is a Java champion, a member of JSR expert groups and a JavaOne rockstar. Hendrik is a member of the Eclipse JakartaEE working group (WG) and the Eclipse Adoptium WG. In addition, Hendrik Ebbers is a member of the Board of Directors of the Eclipse Foundation.

Circle Circle
logo

Open Source made right

Privacy

Privacy Policy Cookie Policy Privacy Config Impressum