I have a few components which I want to check whether or not they are filled in.
My code iterates over every component added to a list, casts it to the correct type, invokes the getText
method and checks whether or not it's empty. In case it's empty, a boolean is set to false and returned. Depending on the boolean, "Yay" (success) or "Nay" (failed) is being shown. As additional testing I have also printed component.toString()
each time it failed.
The problem right now is this: every component.toString()
method is called in a situation where every component has text. Regardless of my input, the boolean remains success (even when some fields have no text in them).
During debugging I've found that none of my components have a field 'Text' or 'getText' or similar in my debugging window (Eclipse), which might be an indication of the problem.
I can't find the problem, can someone point me to it?
boolean success = checkInput();
if (success) {
System.out.println("Yay");
} else {
System.out.println("Nay");
}
checkInput method
private boolean checkInput() {
boolean success = true;
List<Component> components = new ArrayList<Component>();
components.add(txtLeertrajectCode);
components.add(txtLeertrajectTitel);
components.add(txtOmschrijving);
components.add(txtDoelgroep);
for (Component comp : components) {
if (comp instanceof JTextField) {
String curr = ((JTextField) comp).getText().trim();
if ("".equals(curr)) {
System.out.println(comp.toString());
comp.setForeground(Color.RED);
success = false;
} else {
comp.setForeground(Color.BLACK);
}
} else if (comp instanceof JTextArea) {
String curr = ((JTextArea) comp).getText();
if ("".equals(curr)) {
System.out.println(comp.toString());
comp.setForeground(Color.RED);
success = false;
} else {
comp.setForeground(Color.BLACK);
}
}
}
return success;
}
}
Output:
javax.swing.JTextField[...]
javax.swing.JTextField[...]
javax.swing.JTextArea[...]
javax.swing.JTextField[...]
Yay
EDIT:
Problem solved, appearantly I had my textfields twice, but under a similar name in the file. Obviously the checking was done on the textfields not in use. Silly problem.