Javafx: link fx:id-s

I have a UI with many components which could be reused ; so, I have a few .fxml files linked. My parent .fxml embeds this:

<fx:include source="Child.fxml" fx:id="first">
<fx:include source="Child.fxml" fx:id="second">

The Child.fxml looks like this:

<HBox xmlns=""
    <Label fx:id="label"/>
    <ComboBox fx:id="comboBox"/>
    <TextField fx:id="firstTextfield"/>
    <TableView fx:id="secondTextfield"/>

The Parent.fxml has a defined fx:controller=”ParentController”. The question is, how can I set/get the data for each individual child in parent.

first.getLabel().setText("This is the first Label");
second.getLabel().setText("This is the second Label");

Please don’t suggest as answer fist.getChildren().get(0) and others similar methods.

I know I can define only one big .fxml, and then give every single item an id, but I want to avoid to duplicate code, and I want to split them in a smaller components so it may become more understandable, and I can reuse them.

You can inject the controllers for the included FXML into the controller for the FXML that is including them:

public class ParentController {

    private ChildController firstController ; 
    private ChildController secondController ;

    private Pane childContainer ; // container holding the included FXMLs

    // ...

Here I am assuming that Child.fxml declares a controller class fx:controller=”ChildController”. The rule for naming the fields for the nested controllers is that they are the fx:id of the included FXML with “Controller” appended.

Define the appropriate data methods in that controller (it is generally bad practice to allow direct access to the controls themselves):

public class ChildController {

    private Label label ;

    private ComboBox<String> comboBox ;

    // etc...

    public void setDisplayText(String text) {

    public String getUserSelectedValue() {
        return comboBox.getValue();

    // ...

And now back in the ParentController all you need is

first.setDisplayText("This is the first Label");
second.setDisplayText("This is the second Label");


If you need to include more instances of the FXML defined in Child.fxml dynamically at runtime, you just need:

// modify resource name as needed:
FXMLLoader loader = new FXMLLoader(getClass().getResource("Child.fxml"));
Parent childUI = loader.load();
ChildController childController = loader.getController();