This week's book giveaway is in the Agile forum.
We're giving away four copies of Head First Agile and have Andrew Stellman & Jennifer Greene on-line!
See this thread for details.
Win a copy of Head First Agile this week in the Agile forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Disallow certain chars in JTextField  RSS feed

 
Jennifer Sohl
Ranch Hand
Posts: 455
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi. We have certain characters that users should not be allowed to key into a JTextField. They are asterisks(*), double quotes(") and question marks(?). What would be the simplest way (if there is one) to not allow the user to key these chars. I currently have a custom JTextField that extends PlainDocument. I was thinking maybe, I could create a method like 'setInvalidChars(String[] chars) in the custom class. However, I'm not sure what to do with the String if it happens to be an invalid character.
Does that sound like it would work.
Any thoughts are appreciated.
Thanks!
 
Bill Liteplo
Ranch Hand
Posts: 88
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Make a class that implements DocumentListener. This will call one of your methods whenever text is entered:

There are probably other ways. Depends if you want the bad characters never to be entered (e.g. you check after every character) or only upon enter (action event, focus change, etc.). See class javax.swing.InputVerifier.
Bill
 
Falshon Hauxe
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'd been playing with a class that does something similar. Code is below. Basically I use TextListener() to check for changes to my textfield. For every change I parse the string to remove unwanted chars. Then I replace the textfields old value with the new parsed value.
The problem I've encountered with my approach is that my little test app goes into an infinite loop when I try to reset the textfield value.
The TextListener picks up the change and tries to parse it again... Yikes
So I'm trying to disable the TextListener now while calling the method that parses and replaces the TextField's value. It DOES sort of work. Try running the code below. But when I attempt to re-enable the listener (by uncommenting out the line I've commented out //Record1.addTextListener(this); ) the whole thing acts as though I never disabled it .. and there we go with the infinite loop again.
Any suggestions as to what I'm doing wrong are welcome.

[ November 13, 2002: Message edited by: Falshon Hauxe ]
[ November 13, 2002: Message edited by: Falshon Hauxe ]
 
Jennifer Sohl
Ranch Hand
Posts: 455
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Just wanted to let you all know, I found a solution to my problem. Thanks for the suggestions. I ended up registering a KeyListener on my JTextField. If anyone is curious, here's my code:

Works great!
Thanks again!
 
Falshon Hauxe
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I also went the KeyAdapter route and was coming back here to post my fix but see you beat me to it. Nice touch there with the 'beep'. I added that to mine.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!