Search code examples
javafxfxml

Creating an empty VBox in FXML and later adding nodes in javafx doesn't work


In my FXML i have a simple empty VBox inside an AnchorPane:

<VBox fx:id="clients" spacing="10" AnchorPane.rightAnchor="50.0" AnchorPane.topAnchor="150.0">
        <children>
        </children>
</VBox>

Then I want to add a node to the VBox in java class:

clients.getChildren().add(0, customObject.getGridPane());

But the node doesn't show up, VBox keeps empty.

If I simply add any node to the FXML then later adding a node in java works but not if it starts empty.

This works:

<VBox fx:id="clients" spacing="10" AnchorPane.rightAnchor="50.0" AnchorPane.topAnchor="150.0">
        <children>
            <Label />
        </children>
</VBox>

It must be an easy thing I'm missing but couldn't find the answer anywhere.


Solution

  • use addAll method instead of add

    clients.getChildren().addAll(0, customObject.getGridPane());