29

In this tutorial, there 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 the 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(); 
}
2
  • 1
    The second answer is the right solution for this question, It should be marked "Accepted" Commented Aug 16, 2014 at 11:31
  • 1
    @AmirArad Which one is the "second one"? Commented Jul 14, 2017 at 11:41

2 Answers 2

43

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

Sign up to request clarification or add additional context in comments.

1 Comment

Also, mark your own answer as accepted, to make it more visible as the solution to your problem. Just click the tick next to this answer.
2

There seems to be a bug in netbeans 8.0 with nested fxmls as well. Cannot count on netbeans to create the nested fxml's controller object for you, it has to be manually inserted into your MainController. Every time the controller is updated in netbeans it gets wiped out so it can be kind of tedious. For this example that would be inserting the

@FXML private DialogController dialog1Controller;

line manually into the main controller in this case, then it works normally. Very useful for organizing large fxmls/controllers.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.