Search code examples
javafxmodel-view-controllerjavafx-8fxmlvbox

Is there a way to insert children at specific position?


thats a part of my fxml:

<VBox>
  <children>
    <Button/>
    <Button/>
  </children>
</VBox>

I want to add a new child to vBox. But i want to add it at a specific position, for example between the two buttons "at position 2".


Solution

  • The child list of a pane is an ObservableList, which is just a subinterface of a plain old java.util.List. So you have access to all the usual list methods. Assuming you have a reference to the VBox in your controller, call it vbox, you can just do

    vbox.getChildren().add(1, myNewButton);