JavaFx:如何同时在两个GridPanes节点上应用验证? [英] JavaFx: How to apply validation on two GridPanes nodes simultaneously?

查看:148
本文介绍了JavaFx:如何同时在两个GridPanes节点上应用验证?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I'm creating an application using Javafx in which in which I'm creating two different GridPanes say GridPane A and GridPane B. In GridPane A I've TextFields and GridPane B I've TextFields, Checkboxes and DatePickers, all the nodes I'm creating at run time. Under both of the GridPanes A and B I've a button like this:

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

What I want is to disable the button until both the entries of both GridPane A and GridPane B. I'm able on achieve it but only on GridPane A like this

首先检查GridPane A的所有TextField是否已填充且不为空:

First by checking if all the TextFields of GridPane A are filled and not empty:

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)并添加监听器:

Then validating the TableA (GridPane A) and adding listener:

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条目都被填充。

I want to apply validation simultaneously on both GridPane A and GridPane B, like right now my program is validating GridPane A entries and disabling/enabling the button but I want is to keep button disabled unless both GridPane A and GridPane B entries are filled.

推荐答案

检查所有 TextFields 是否已填充的方式相同表A中的,你可以对第二个表(表B)做同样的事情:

The same way you're checking if all TextFields are Filled in Table A, You can do the same for the second Table (Table 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
}

现在每次更改 TextField TableA TableB 中,它将检查是否所有 TextFields 在两个表格中已填充

Now with every change in any TextField either in TableA or TableB, it will check if all TextFields in both tables are Filled.

然后将tableB添加到 validateTable 方法,如下所示:

Then Add tableB to the validateTable method, like this:

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);
            }
         });
       }    
    }
}

这篇关于JavaFx:如何同时在两个GridPanes节点上应用验证?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆