• Post Reply Bookmark Topic Watch Topic
  • New Topic

length of a jtextfield  RSS feed

 
Mike Mattos
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi

is it possible to limit the lenght of a JtextField?

thanks
 
Ulf Dittmer
Rancher
Posts: 42972
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm not sure if Swing offers that out of the box, but your code can reject inputs that are not to its liking; see http://faq.javaranch.com/java/NumericTextField for a JTextField that rejects non-numerical characters.
 
Rob Spoor
Sheriff
Posts: 21094
85
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Using a custom Document can help you, but so can using DocumentFilter. Both work in a similar way: if the String to add would cause the total length to be exceeded, either reject the entire String or use a substring that would cause the total length to be met exactly.

Moving to our GUI forum.
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!