JavaFx:如何同时在两个GridPanes节点上应用验证?

时间:2017-05-31 20:42:55

标签: java javafx javafx-2 javafx-8

我正在使用Javafx创建一个应用程序,其中我正在创建两个不同的GridPanes,表示GridPane A和GridPane B.在GridPane中我有TextFields和GridPane B我有TextFields,Checkboxes和DatePickers,所有这些我在运行时创建的节点。在GridPanes A和B下,我有一个这样的按钮:

enter image description here

我想要的是禁用按钮,直到GridPane A和GridPane B的两个条目都有。我能够实现它,但只能在GridPane A上这样做

首先检查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;
  }

然后验证TableA(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 B上同时应用验证,就像现在我的程序正在验证GridPane A条目和禁用/启用按钮但我想要保持按钮被禁用,除非GridPane A和GridPane B条目都是填充。

1 个答案:

答案 0 :(得分:1)

您在表A中检查所有TextFields是否已填充的方式相同,您可以对第二个表(表B)执行相同操作:

// 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
}

现在,对于 TableA TableB 中的任何TextField的每次更改,它都会检查两个表中的所有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);
            }
         });
       }    
    }
}
相关问题