• 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

detect textfield

 
Ranch Hand
Posts: 242
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I have a JtextField. How do I detect if the user has entered just a space bar (in the TextField) and proceed. I want to detect and stop the user from proceeding further. Thanks.
I tried like this. But this doesn't stop the user.

[ April 17, 2008: Message edited by: Gopu Akraju ]
 
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Try this:


[ April 17, 2008: Message edited by: stef mosh ]
 
Author
Posts: 986
3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Gopu Akraju:
String name = nameTextField.getText();
name.trim();
if(name == null || name == (" ") || name.length() ==0 || name ==("\\s")){
//Stop the user
}



String.trim() doesn't do what you think it does.

Strings are immutable. Try name = name.trim() instead. In that case:

a) name == null can't be true or else name.trim() would have thrown NullPointerException.
b) name == (" ") can't be true because it has been trim()ed.
c) name.length() ==0 is valid and is all you really need.
d) name ==("\\s") is testing for the literal name \s. The == operator has nothing to do with regular expressions. See String.matches().
[ April 17, 2008: Message edited by: Brian Cole ]
 
I'm not dead! I feel happy! I'd like to go for a walk! I'll even read a tiny ad:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic