• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Recognising when nothing entered in JTextField

 
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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??
 
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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!!
}
}
 
Ranch Hand
Posts: 15304
6
Mac OS X IntelliJ IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
reply
    Bookmark Topic Watch Topic
  • New Topic