2017-05-29 41 views
0

如何檢查是否裏面anchorpane有一些節點存在或者不使用觀察或右裝訂現在,我使用if語句是這樣的:JavaFx:如何在應用程序運行時觀察Anchorpane?

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,它會在應用程序的運行狀態中更改。

回答

0

您可以創建一個布爾可觀察名單是否爲空結合:

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

然後:

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