/ DataFX / DataFX Controller API

DataFX Controller API

Hendrik on 2013/09/13 - 19:16 in DataFX, JavaFX

The DataFX 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"/>
</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)

3 POST COMMENT

Send Us A Message Here

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

3 Comments