• Post Reply Bookmark Topic Watch Topic
  • New Topic

Recognising when nothing entered in JTextField  RSS feed

 
kathy o'connell
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I want to be able to pick up when nothing has been entered into a JTextField. I have tried checking for a null string, but this does not work. Any ideas??
 
Alex Draper
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Well, one would be the following:
textField.getText().trim().lenght(); //some where in your code

The other would be to add a keyListener to the textField
and check for the following when a key has been Released.
<pre>
public void keyReleased(KeyEvent e) {

if (textField.getText().trim().length() == 0) {
//text is empty
} else if (textField.getText().trim().length() > 0) {
//It has Text!!
}
}
 
Gregg Bolinger
Ranch Hand
Posts: 15304
6
Chrome IntelliJ IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You can do it a couple of ways.
if (MyTextArea.getText().compareTo("") == 0) {
//There is no text here
}
or you can do
String myText = myTextArea.getText();
and then compare myText the same way as above.

------------------
Happy Coding,
Gregg Bolinger
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!