Win a copy of Kotlin in Action this week in the Kotlin forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

JTextField length restriction...  RSS feed

 
Sathish Babu Adluru Subas
Greenhorn
Posts: 26
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi javacs,
Supposing,i want to restrict the user input in the JTextField to 10 chrs.How can i do it?.
thanx in advance.
 
Anonymous
Ranch Hand
Posts: 18944
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hello,
have you got a solution to this?
I am doing it by extending PlainDocument and manipulating the string parameter ,but there may be a better way.
regards,
suchitra
 
Tom P
Ranch Hand
Posts: 88
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Capture key strokes and when the user is typing in the 11th character consume() the event.
 
Anonymous
Ranch Hand
Posts: 18944
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello,
I have been doing screen validations for AWT components this way
But this is not working for swing component like the JTextBox.
the keyEvent.consume() method is not being called!
regards,
suchitra
 
Greg Whelan
Ranch Hand
Posts: 52
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If you'd like a book reference, you'll probably find O'Reilly's Swing book helpful on this matter. In fact an extension of PlainDocument is provided that limits the length of the content (they even point out that this is a common question ) It's discussed on pages 772-774.
You can obtain the sample code at:
ftp://ftp.oreilly.com/pub/examples/java/swing/
(choose your preferred compressed format from the list).
The relevant files are ch20/FixedLength*
The keystroke capture approach is the AWT-ish way of doing things. The Swing way is to create a specialized Document -- specifically an extension of PlainDocument. By doing so a wider range of situations are handled -- such as copying from a clipboard or having other parts of your program perform an insertion.
As a note of caution to the O'Reilly book, in places they have a tendency to give blow-by-blow accounts of the API in excruciating detail. There's quite a bit in Ch.20 to trudge through before reaching the discussion of the relatively simple PlainDocument (and how to extend it).
 
Anonymous
Ranch Hand
Posts: 18944
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
thanx Greg ,
this information is really very useful
regards,
suchitra
 
Don't get me started about those stupid light bulbs.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!