• Post Reply Bookmark Topic Watch Topic
  • New Topic

Help on JTextField On Change Validation

 
Rudy Yeung
Ranch Hand
Posts: 183
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Please advise if this is the easiest way to limit the text field to 3 chars in length only.
1. added keylisterner to a JTextField instance
2. in the keyTyped (possibly keyPressed) method do the following:
{
grep the length of the text field input so far
if the length is more than 3 then grep only the first 3 chars of the field
use setText() to put the first three chars back to the text field
}
 
Rudy Yeung
Ranch Hand
Posts: 183
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I finally sove it myself. I create a class that extends the PlainDocument. Inside this new class, I do whatever checking I like, and that instantiate it and pass the instance to the JTextField constructor.
 
deekasha gunwant
Ranch Hand
Posts: 396
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Rudy,
yes u opted for the perfect solution.
deeksha
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!