如何在Tab JavaFX内部向HBox添加节点?

时间:2016-11-28 16:38:51

标签: java javafx tabs

我有一个我创建的GUI,我想在应用程序在后台执行某些操作时添加ProgressIndicator。我在构造函数中创建了一个Tab,类似于以下内容:

public class myGUI {
    Tab myTab;

    myGUI() {
        myTab = new Tab("My Tab");

        HBox view = new HBox();
        VBox left = new VBox();
        BorderPane right = new BorderPane();

        /*A lot of other things are declared that go in left and right*/

        view.getChildren().addAll(left, right);
        myTab.setContent(view);
    }
...

稍后,我按下按钮启动应用程序执行后台任务,我想在ProgressIndicator的中心添加BorderPane。我试过以下的东西:

private void handleMyAction(MouseEvent e) {
    myTab.getContent().getChildren().get(1).setCenter(new ProgressIndicator(-1.0f));
}

我认为这样可行,getContent会返回Node,而我无法在getChildren上调用Node。如何在不使BorderPane成为我班级中的字段的情况下访问Node以添加其他BorderPane

1 个答案:

答案 0 :(得分:1)

只需将边框窗格设为实例变量:

public class MyGUI {
    private Tab myTab;
    private BorderPane right ;

    MyGUI() {
        myTab = new Tab("My Tab");

        HBox view = new HBox();
        VBox left = new VBox();
        right = new BorderPane();

        /*A lot of other things are declared that go in left and right*/

        view.getChildren().addAll(left, right);
        myTab.setContent(view);
    }

    private void handleMyAction(MouseEvent e) {
        right.setCenter(new ProgressIndicator(-1.0f));
    }
}