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

JavaFx:如何同时在两个GridPanes节点上应用验证?,java,javafx,javafx-2,javafx-8,Java,Javafx,Javafx 2,Javafx 8,我正在使用Javafx创建一个应用程序,在其中我创建了两个不同的网格,分别是GridPane A和GridPane B。在GridPane A中,我有文本字段和GridPane B,我有文本字段、复选框和日期选择器,我在运行时创建的所有节点。在网格窗格A和B下,我有一个如下按钮: 我想要的是禁用该按钮,直到GridPane A和GridPane B的两个条目都出现为止。我可以实现它,但只能在GridPane A上实现 首先,通过检查GridPane A的所有文本字段是否已填充且不为空: pri

我正在使用Javafx创建一个应用程序,在其中我创建了两个不同的网格,分别是GridPane A和GridPane B。在GridPane A中,我有文本字段和GridPane B,我有文本字段、复选框和日期选择器,我在运行时创建的所有节点。在网格窗格A和B下,我有一个如下按钮:

我想要的是禁用该按钮,直到GridPane A和GridPane B的两个条目都出现为止。我可以实现它,但只能在GridPane A上实现

首先,通过检查GridPane A的所有文本字段是否已填充且不为空:

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(网格窗格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 
            }
        });
     }    
  }

  }

我想同时在网格窗格A和网格窗格B上应用验证,就像现在一样,我的程序正在验证GridPane A条目并禁用/启用按钮,但我想让按钮保持禁用状态,除非GridPane A和GridPane B条目都已填写。

与检查表A中是否填写了所有
文本字段的方法相同,您可以对第二个表(表B)执行相同的操作:

现在,随着表A或表B中任何
TextField
的每次更改,它将检查两个表中的所有
TextField
是否都已填充


然后将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);
            }
         });
       }    
    }
}

从可用性的角度来看,您可能需要考虑在任何时候启用按钮,并且如果用户在任何文本字段为空时按下它,则显示带有信息性消息的AN。用户可能不清楚为什么按钮被禁用或如何使其启用。你是对的,但这是项目的要求。它起作用了merci@Yahya:)另一件事如果我想验证复选框和日期选择器,我必须这样做
if(node instanceof TextField&&node instanceof CheckBox&&node instanceof DatePicker)
?@Junaid我很高兴能帮上忙:)。关于你的问题,它应该是
if(node instanceof TextField){//add change listener}
并且在它之后添加
else if(node instanceof CheckBox){//add change listener}
等等..好的,和@Yahya我可以相互比较两列textfields值吗?例如,如果第1列TextField值小于第3列TextField值,请保持按钮禁用。并以红色显示具有较大值的指定列3文本字段的边框。两个比较文本字段应该在同一行,但它们可以在不同的列中。@Junaid说实话,我不完全理解你的意思。请您提供一张
gif
图像或任何其他您想要达到的效果(请记住,我没有您所有的代码,因此我可能不完全了解完整的图像/也没有更新您在项目中所做的所有更改)。好的@Yahya我将在另一个问题中发布它。
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);
            }
         });
       }    
    }
}