2017-05-31 60 views
1

我正在創建一個應用程序,使用Javafx在其中我創建兩個不同的GridPanes說GridPane A和GridPane B.在GridPane A我有TextFields和GridPane B我有TextFields,Checkboxes和DatePickers,所有節點我在運行時創建。同時根據GridPanes A和B的我有一個按鈕,像這樣:JavaFx:如何同時在兩個GridPanes節點上應用驗證?

enter image description here

我想是直到兩個GridPane A和B. GridPane兩個條目我能上禁用按鈕實現它,但只有在GridPane一個這樣

首先通過檢查GridPane A的總的TextField被填充,而不是空:

private static boolean isAllFilled(GridPane tableA){ 
    for(Node node : tableA.getChildren()){ // cycle through every component in the table (GridPane) 
     if(node instanceof TextField){ // if it's a TextField 
     // after removing the leading spaces, check if it's empty 
      if(((TextField)node).getText().trim().isEmpty()){ 
        return false; // if so, return false 
      } 
     }  
    } 
    return true; 
    } 

然後驗證表A(GridPane A)和添加的偵聽:

private void validateTable(GridPane tableA, Button button) { 

    for(Node node : tableA.getChildren()){ // cycle through every component in the table (GridPane) 
     if(node instanceof TextField){ // if it's a TextField 
      ((TextField)node).textProperty().addListener((obs, old, newV)->{ // add a change listener to every TextField 
      // then check if the new value is not empty AND all other TextFields are not empty 
      if(!newV.trim().isEmpty()&&isAllFilled(tableA)){ 
       button.setDisable(false); // then make the button active again 
      } 
      else{ 
       button.setDisable(true); // or else, make it disable until it achieves the required condition 
      } 
     }); 
    }  
    } 

    } 

我想在這兩個GridPane A和GridPane乙同時應用驗證,就像現在我的計劃是驗證GridPane的參賽作品和禁用/啓用按鈕,但我要的是保持,除非兩個GridPane A和GridPane禁用按鈕B條目已填寫。

+0

從實用性的角度來看,你可能要考慮留在任何時候啓用按鈕,如果用戶按下它當任何文本字段爲空時,顯示[錯誤警報](http://docs.oracle.com/javase/8/javafx/api/javafx/scene/control/Alert.AlertType.html#ERROR)信息性消息。用戶可能不明白爲什麼該按鈕被禁用或如何使其被啓用。 – VGR

+0

你是對的,但它是項目的要求。 – Junaid

回答

1

如果所有TextFields表的填充,您可以爲第二個表做同樣的,你正在檢查以同樣的方式(表二):

// cycle through every component in tableA 
private static boolean isAllFilled(GridPane tableA, GridPane tableB){ 
    for(Node node : tableA.getChildren()){ 
     if(node instanceof TextField){ 
      if(((TextField)node).getText().trim().isEmpty()){ 
        return false; 
      } 
     }  
    } 

    // cycle through every component in tableB 
    for(Node node : tableB.getChildren()){ 
     if(node instanceof TextField){ 
      if(((TextField)node).getText().trim().isEmpty()){ 
        return false; 
      } 
     }  
    } 

    return true; // if all are filled/not empty 
} 
以任何 TextField每一個變化

現在或者在表A表B,它將檢查兩個表中的所有TextFields是否爲填充的


然後tableB的添加到validateTable方法,像這樣:

private void validateTable(GridPane tableA, GridPane tableB, Button button){ 

    // add a change listener to every TextField in tableA 
    for(Node node : tableA.getChildren()){ 
     if(node instanceof TextField){ 
      ((TextField)node).textProperty().addListener((obs, old, newV)->{ 
      if(!newV.trim().isEmpty()&&isAllFilled(tableA, tableB)){ 
       button.setDisable(false); 
      } 
      else{ 
       button.setDisable(true); 
      } 
     }); 
     }  
    } 

    // add a change listener to every TextField in tableB 
    for(Node node : tableB.getChildren()){ 
     if(node instanceof TextField){ 
      ((TextField)node).textProperty().addListener((obs, old, newV)->{ 
      if(!newV.trim().isEmpty()&&isAllFilled(tableA, tableB)){ 
       button.setDisable(false); 
      } 
      else{ 
       button.setDisable(true); 
      } 
     }); 
     }  
    } 
} 
+0

它工作merci @Yahya :)另一件事,如果我想驗證CheckBox和DatePicker,我必須這樣做'if(node instanceof TextField && node instanceof CheckBox && node instanceof DatePicker)'? – Junaid

+0

@Junaid我很高興我可以幫助:)。關於你的問題,它應該是'if(node instanceof TextField){//添加變化監聽器}'並且在它添加'else if(節點instanceof CheckBox){//添加變化監聽器}'等等。 – Yahya

+0

好吧,和@Yahya我可以比較兩個列textfields值彼此,例如如果列1 TextField值小於列3 TextField值保持按鈕禁用。並顯示具有較大紅色值的特定列3文本字段的邊框。兩個比較文本域應該在同一行上,但它們可以在不同的列中。 – Junaid

相關問題