Adam
Adam

Reputation: 319

How to show a notification to the user if a textfield is left blank

How can you show a JOptionPane notification to the user when a textfield is left blank?

I am new to Java

I made a button to submit a name, and if the user submits a blank, it gets a blank also. Is there a certain method from the textfield to determine whether the textfield is blank or not, so I can give alert that the textfield is blank from JOptionPane?

Upvotes: 0

Views: 1822

Answers (1)

JB Nizet
JB Nizet

Reputation: 691635

In the button's ActionListener:

if (textField.getText().isEmpty()) {
    JOptionPane.showMessageDialog(textField, "Beware: you left the text field blank");
}
else {
    proceedAsUsual();
}

Upvotes: 2

Related Questions