Aldrin Berana
Aldrin Berana

Reputation: 1

How to put an asterisk sign beside a text field if the text field is empty?

How it will work? If i press the button and the text field is empty it will display a asterisk sign besides the textfiled.

if(txtfname.getText().equals(" ")){
  JOptionPane.showMessageDialog(null, "Missing field");
  jLabel20.setText("*");
}

Upvotes: 0

Views: 1106

Answers (1)

Amit Deshpande
Amit Deshpande

Reputation: 19185

if(txtfname.getText().trim().isEmpty())//Trim removes unnecessary chars is empty checks for emptyness
//show *

Remember trim() on null string can generate NullPointerException so you should also check for null before.

Upvotes: 4

Related Questions