JavaFx Nested Controllers (FXML )


Question

In this tutotial, is an example of how to include custom components and use their controllers from the controller of the container.

main_window_content.fxml

<VBox fx:controller="com.foo.MainController">
   <fx:include fx:id="dialog" source="dialog.fxml"/>
   ...
</VBox>

MainController.java

public class MainController extends Controller {
    @FXML private Window dialog;
    @FXML private DialogController dialogController;

    ..

If the component is included only once, it works fine. If the same component is included twice, the controllers are not initialized. Both controllers are null.

main_window_content.fxml

    <VBox fx:controller="com.foo.MainController">
       <fx:include fx:id="dialog1" source="dialog.fxml"/>
       <fx:include fx:id="dialog2" source="dialog.fxml"/>
       ...
    </VBox>

MainController.java

    public class MainController extends Controller {
        @FXML private Window dialog1;
        @FXML private DialogController dialogController1;
        @FXML private Window dialog2;
        @FXML private DialogController dialogController2;

Could someone help me to solve the problem? thanks

This is my FXML loading code. It is executed in main application method:

public void start(Stage stage) throws Exception { 
    Parent root = FXMLLoader.load(getClass().getResource("main_window_content.fxml"));
    stage.setTitle("FXML Welcome"); 
    stage.setScene(new Scene(root, 300, 275));
    stage.show(); 
}
1
27
8/26/2014 6:22:30 AM

Thanks to Daniel (from OTN) I found the error in my code, the names of my controller variables were wrong. They should be <fx:id>Controller. In other words it should be:

MainController.java

public class MainController extends Controller {
@FXML private Window dialog1;
@FXML private DialogController dialog1Controller;
@FXML private Window dialog2;
@FXML private DialogController dialog2Controller;

But studying the changes introduced in version 2.2 I found that everything can be easily solved by using <fx:root> tag (like this tutorial). I entered my component in FXML simply declaring it like this:

<HBox>
    <Dialog id="dialog1" text="Hello World!"/>
    <Dialog id="dialog2" text="Hello World!"/>
</HBox>

I hope to be helpful

36
12/18/2015 5:23:57 PM

Licensed under: CC-BY-SA with attribution
Not affiliated with: Stack Overflow
Icon