Search code examples
javajavafxfxml

Insert the same object several times in FXML file (JavaFX)


I have a FXML file with 2 tabs. In each tab, I have the same list of Text elements. How to avoid having to duplicate each Text element?

Here is an extract of my FXML file:

<Tab>
    <GridPane>
        <columnConstraints>
            <ColumnConstraints />
        </columnConstraints>
        <rowConstraints>
            <RowConstraints />
            <RowConstraints />
            <RowConstraints />
            <RowConstraints />
        <RowConstraints />
        </rowConstraints>
        <children>
            <Text fx:id="text1" GridPane.rowIndex="1" />
            <Text fx:id="text2" GridPane.rowIndex="2" />
            <Text fx:id="text3" GridPane.rowIndex="3" />
            <Text fx:id="text4" GridPane.rowIndex="4" />
        </children>
    </GridPane>
</Tab>
<Tab>
    <GridPane>
        <columnConstraints>
            <ColumnConstraints />
        </columnConstraints>
        <rowConstraints>
            <RowConstraints />
            <RowConstraints />
            <RowConstraints />
            <RowConstraints />
        <RowConstraints />
        </rowConstraints>
        <children>
            <Text fx:id="text1" GridPane.rowIndex="1" />
            <Text fx:id="text2" GridPane.rowIndex="2" />
            <Text fx:id="text3" GridPane.rowIndex="3" />
            <Text fx:id="text4" GridPane.rowIndex="4" />
        </children>
    </GridPane>
</Tab>

If I put the same id in two Text elements (for example: fx:id="text1" in the two tabs), I have an error (Duplicate id reference).


Solution

  • Ok, I based on this solution which explains how to load items from the Java code. And if I load 2 times the same item (one time in the first tab and a second time in the second tab), it's only displayed on the second tab... So an item can only be displayed once. So I just create 2 identicals items.

    Here is a part of my FXML code:

    <Tab>
        <GridPane>
            <columnConstraints>
                <ColumnConstraints />
            </columnConstraints>
            <rowConstraints>
                <RowConstraints />
                <RowConstraints />
                <RowConstraints />
                <RowConstraints />
            <RowConstraints />
            </rowConstraints>
            <children>
                <!-- I add my Text elements from JavaFX code -->
            </children>
        </GridPane>
    </Tab>
    <Tab>
        <GridPane>
            <columnConstraints>
                <ColumnConstraints />
            </columnConstraints>
            <rowConstraints>
                <RowConstraints />
                <RowConstraints />
                <RowConstraints />
                <RowConstraints />
            <RowConstraints />
            </rowConstraints>
            <children>
                <!-- I add my Text elements from JavaFX code -->
            </children>
        </GridPane>
    </Tab>
    

    And here is a part of my Java code:

    @FXML private GridPane gridPaneFirstTab;
    @FXML private GridPane gridPaneSecondTab;
    
    private List<Text> textCallbacksList1 = new ArrayList<>();
    private List<Text> textCallbacksList2 = new ArrayList<>();
    private final List<String> callbackNames = Arrays.asList(
        "text1",
        "text2",
        "text3"
    );
    
    Text text1, text2;
    for (int i = 0; i < callbackNames.size(); ++i) {
        text1 = new Text(MessagesBundle.getString(callbackNames.get(i)));
        text2 = new Text(MessagesBundle.getString(callbackNames.get(i)));
        textCallbacksList1.add(text1);
        textCallbacksList2.add(text1);
    
        gridPaneFirstTab.getChildren().add(text1);
        gridPaneSecondTab.getChildren().add(text2);
    
        gridPaneFirstTab.setConstraints(text1, 0, i + 1);
        gridPaneSecondTab.setConstraints(text2, 0, i + 1);
    }