1

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:

enter image description here

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.

Junaid
  • 664
  • 5
  • 18
  • 35
  • From a usability perspective, you may want to consider leaving the button enabled at all times, and if the user presses it when any of the text fields are empty, show an [error Alert](http://docs.oracle.com/javase/8/javafx/api/javafx/scene/control/Alert.AlertType.html#ERROR) with an informative message. It might not be obvious to the user why the button is disabled or how to cause it to be enabled. – VGR May 31 '17 at 21:04
  • You're right but it's requirement of the project. – Junaid May 31 '17 at 21:29

1 Answers1

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);
            }
         });
       }    
    }
}
Yahya
  • 13,349
  • 6
  • 30
  • 42
  • it worked merci @Yahya :) Another thing if I want to validate CheckBox and DatePicker, I've to do like this `if(node instanceof TextField && node instanceof CheckBox && node instanceof DatePicker)` ? – Junaid May 31 '17 at 21:27
  • @Junaid I'm glad I could help :). Regarding you question, it should be `if(node instanceof TextField){// add change listener}` and after it add `else if(node instanceof CheckBox){// add change listener}` and so on.. – Yahya May 31 '17 at 21:33
  • Okay, and @Yahya can I compare two column textfields values with each other for example if column 1 TextField value is lesser than column 3 TextField value keep the button disabled. and show border of that specif column 3 textfield which has greater value in red color. two comparing textfields should be on the same row but they can be in different columns. – Junaid Jun 01 '17 at 14:55
  • @Junaid To be honest I did not understand what you mean fully. Would you please provide a `gif` image or any other draw of what you want to achieve (Please remember that I don't have all your code so I may not be fully aware of the complete image / nor updated with all changes you did in your project). – Yahya Jun 01 '17 at 15:00
  • Ok @Yahya I'll post it in another question. – Junaid Jun 01 '17 at 15:09
  • [Question](https://stackoverflow.com/questions/44312354/javafx-how-to-compare-values-of-dynamically-created-textfields-inside-gridpane) @Yahya I've posted a question. – Junaid Jun 01 '17 at 16:05