英文:
How to get StackPane's child using the child's variable name
问题
TabPane tabpaneSubs = new TabPane();
stackPane.getChildren().add(tabpaneSubs);
button.setOnAction(actionEvent -> {
tabpaneSubs.toFront();
}
英文:
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
得分: 1
这应该可以正常工作,因为你在编译时已经知道你的 TabPane
是一个子节点:
button.setOnAction(actionEvent -> {
tabpaneSubs.toFront();
});
更一般地,你应该使用 id
属性来找到特定的元素:
tabpaneSubs.setId("my-tab-pane");
button.setOnAction(actionEvent -> {
for(Node node : stackPane.getChildren()) {
if("my-tab-name".equals(node.getId())){
node.toFront();
return;
}
}
});
英文:
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;
}
}
});
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论