Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Create a function in java check form

Tags:

java

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;
        }
like image 663
Hac Bao Avatar asked May 14 '26 23:05

Hac Bao


1 Answers

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.

like image 97
dsh Avatar answered May 17 '26 13:05

dsh



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!