JavaFx: How to apply validation on two GridPanes nodes simultaneously?

大憨熊 提交于 2019-12-11 06:34:59

问题


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:

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

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

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

  }

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.


回答1:


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
}

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


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


来源:https://stackoverflow.com/questions/44294321/javafx-how-to-apply-validation-on-two-gridpanes-nodes-simultaneously

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!