0

Am having a method that check all JTextFields in a panel JPanel to see whether they are empty, am iterating through all the components in the container. in the container i have labels, text fields and combo boxes. So i can be able validate the first few JTextFields but when i encounter the first JComboBox<?> the validation stop and i don't seem to understand why. Below is the code :-

private boolean validateInputFields(JPanel container) {
    for (Component comp : container.getComponents()) {

        if (comp instanceof JTextField) {
            JTextField temp = (JTextField) comp;
            if (temp.getText().trim().equals("")) {
                changeComponentProperties(temp);
                return true;
            } else{
                temp.setBackground(Color.WHITE);
                temp.setForeground(Color.BLACK);
            }
        }
    }

    return false;
}

Any assistance will be highly appreciated.

Also note that this is invoked when a button (say save button) is clicked.

tmwanik
  • 1,643
  • 14
  • 20
  • 1
    Consider using a `JFormattedTextField`. This should allow you to change the background color immediately when invalid input is entered. See [this answer](http://stackoverflow.com/a/13424140/1076463) for an example – Robin Nov 27 '12 at 09:22
  • Alot of thanks for all who contributed, all the solutions you provided were valid and workable but in my situation i figure out the problem - The thing is some components were not visible in the screen and while iteration through the fetched components they were also not included, so i added a condition to check the component visibility state i.e `comp.isVisible()`. – tmwanik Mar 23 '13 at 00:59

3 Answers3

1

So i can be able validate the first few JTextFields but when i encounter the first JComboBox<?> the validation stop and i don't seem to understand why

I doubt that is the case. I think your loop stops the first time you encounter a JTextField with an empty string as context. In that case you enter the following if

if (temp.getText().trim().equals("")) {
  changeComponentProperties(temp);
  return true;
}

and the return statement causes you to exit your loop. Adjusting it to the following should do the trick

private boolean validateInputFields(JPanel container) {
   boolean result = false; 
   for (Component comp : container.getComponents()) {

        if (comp instanceof JTextField) {
            JTextField temp = (JTextField) comp;
            if (temp.getText().trim().equals("")) {
                changeComponentProperties(temp);
                result = true;
            } else{
                temp.setBackground(Color.WHITE);
                temp.setForeground(Color.BLACK);
            }
        }
    }
    return result;
}
Robin
  • 36,233
  • 5
  • 47
  • 99
0
private boolean validateInputFields(JPanel container) {
for (Component comp : container.getComponents()) {
if (!comp instanceof JTextField) {
continue;
}
else{
JTextField temp = (JTextField) comp;
if (temp.getText().trim().equals("")) {
changeComponentProperties(temp);
return true;
} else{
temp.setBackground(Color.WHITE);`enter code here`
temp.setForeground(Color.BLACK);
}
}
}
}
return false;
}
abishkar bhattarai
  • 7,371
  • 8
  • 49
  • 66
0

Alot of thanks for all who contributed, all the solutions you provided were valid and workable but in my situation i figure out the problem - The thing is some components were not visible in the screen and while iteration through the fetched components they were also not included, so i added a condition to check the component visibility state i.e comp.isVisible().

tmwanik
  • 1,643
  • 14
  • 20