I want to create a function to check input values for the JTextField or JTextArea in java with empty or not
public boolean checkFormEmpty(List<? extends JTextComponent> lsts) {
for (JTextComponent lst : lsts) {
if (!lst.getText().isEmpty()) {
return true;
}
}
return false;
}
When I add an input variable values can only check it.It is valid only when all JTextComponent (txtEmail && txtPassword && txtAreaContent) is empty.
I wish if an input box is blank (txtEmail || txtPassword || txtAreaContent) it still works.Please help me
List<JTextComponent> lst=new ArrayList();
lst.add(txtEmail);
lst.add(txtPassword);
lst.add(txtAreaContent);
if (!checkFormEmpty(lst)) {
JOptionPane.showMessageDialog(this, "Please complete the data");
return;
}
You short-circuit the tests, but you did it opposite what you want.
for (JTextComponent lst : lsts) {
if (lst.getText().isEmpty()) {
return false;
}
}
return true;
You had it stopping and declaring everything is valid when it first found an input that was not empty. Instead you want to stop and declare failure when you find an empty one, but continue checking all of them when it is not empty.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With