JavaFx:如何在应用程序 运行 时观察 Anchorpane?

JavaFx: How to observe Anchorpane while application is running?

现在如何使用 observable 或 binding 检查锚窗格内是否存在某个节点我正在使用这样的 if 语句:

if(anchorPane().getChildren().size()>0){
    GridPane table = (GridPane) anchorPane().getChildren().get(0);
    for(int i=1 ; i<=ComboBox().getValue(); i++){
        TextField Text0 = ((TextField)getComponent (i, 0, table));
        TextField Text1 = ((TextField)getComponent (i, 1, table));
        TextField Text2 = ((TextField)getComponent (i, 2, table));
        TextField Text3 = ((TextField)getComponent (i, 3, table));
        TextField Text4 = ((TextField)getComponent (i, 4, table));
        TextField Text5 = ((TextField)getComponent (i, 5, table));
        TextField Text6 = ((TextField)getComponent (i, 6, table));
        TextField Text7 = ((TextField)getComponent (i, 7, table));

        System.out.println(Text0 + " " + Text1 + " " + Text2 + " " + Text3 + " " + Text4 + " " + Text5 + " " + Text6 + " " + Text7);

            }
    }

我想实现这一点,因为 anchorPane().getChildren().size() 不会始终保持为 0,它会在应用程序的 运行 状态期间发生变化。

您可以为可观察列表是否为空创建布尔绑定:

BooleanBinding empty = Bindings.isEmpty(anchorPane.getChildren());

然后:

empty.addListener((obs, wasEmpty, isNowEmpty) -> {
    if (! isNowEmpty) {
        // ...
    }
});