Java - Check if JTextField is empty or not
For that you need to add change listener (a DocumentListener
which reacts for change in the text) for your JTextField
, and within actionPerformed()
, you need to update the loginButton
to enabled/disabled depending on the whether the JTextfield
is empty or not.
Below is what I found from this thread.
yourJTextField.getDocument().addDocumentListener(new DocumentListener() {
public void changedUpdate(DocumentEvent e) {
changed();
}
public void removeUpdate(DocumentEvent e) {
changed();
}
public void insertUpdate(DocumentEvent e) {
changed();
}
public void changed() {
if (yourJTextField.getText().equals("")){
loginButton.setEnabled(false);
}
else {
loginButton.setEnabled(true);
}
}
});
What you need is something called Document Listener. See How to Write a Document Listener.
To Check JTextFiled is empty or not condition:
if( (billnotf.getText().length()==0)||(billtabtf.getText().length()==0))
The following will return true if the JTextField name
does not contain text:
name.getText().isEmpty();