Win a copy of Murach's Python Programming this week in the Jython/Python forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

detect textfield  RSS feed

 
Gopu Akraju
Ranch Hand
Posts: 242
  • Mark post as helpful
  • send pies
  • 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 ]
 
stef mosh
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Try this:


[ April 17, 2008: Message edited by: stef mosh ]
 
Brian Cole
Author
Ranch Hand
Posts: 932
1
  • Mark post as helpful
  • send pies
  • 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 ]
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!