How to get StackPane's child using the child's variable name

Viewed 42
TabPane tabpaneSubs = new TabPane();
stackPane.getChildren().add(tabpaneSubs);

button.setOnAction( actionEvent -> {
     stackPane.getChildren().get(1).toFront();
}

Instead of using .get(1) i want to use the variable's name : tabpaneSubs

How do i do that ?

1 Answers

This should work simply because you already know your TabPane is a child at compile time:

button.setOnAction( actionEvent -> {
     tabpaneSubs.toFront();
});

More generally, you should use the id property to find specific things

tabpaneSubs.setId("my-tab-pane");

button.setOnAction( actionEvent -> {
     for(Node node : stackPane.getChildren()) {
       if("my-tab-name".equals(node.getId()){
         node.toFront();
         return;
       }
     }
});
Related