Am having a method that check all JTextField
s 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 JTextField
s 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.
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()
.