• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Liutauras Vilda
  • Jeanne Boyarsky
  • Devaka Cooray
  • Paul Clapham
Sheriffs:
  • Tim Cooke
  • Knute Snortum
  • Bear Bibeault
Saloon Keepers:
  • Ron McLeod
  • Tim Moores
  • Stephan van Hulst
  • Piet Souris
  • Ganesh Patekar
Bartenders:
  • Frits Walraven
  • Carey Brown
  • Tim Holloway

InputVerifier, Document or DocumentFilter

 
Sheriff
Posts: 11604
178
Hibernate jQuery Eclipse IDE Spring MySQL Database AngularJS Tomcat Server Chrome Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi all,

I googled a bit around to find a solution for my problem "limit number of chars in a JTextField". So the code isn't the problem, but you have (at least) 3 different ways of doing this:
1) an InputVerifier
2) create a specific PlainDocument
3) create a specific DocumentFilter
4) maybe something i didn't find on google

Which is the best solution? Any guidelines in which one to use? Maybe one i must not use because it's really bad...

Kind regards,
Roel
 
Ranch Hand
Posts: 4632
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
> "limit number of chars in a JTextField"

> 1) an InputVerifier

checks input to see if valid

> 2) create a specific PlainDocument

is the old way, see #3

> 3) create a specific DocumentFilter

this is the one you want

> 4) maybe something i didn't find on google

JFormattedTextField
 
Roel De Nijs
Sheriff
Posts: 11604
178
Hibernate jQuery Eclipse IDE Spring MySQL Database AngularJS Tomcat Server Chrome Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Michael,

Thanks for your quick reply. I will use the DocumentFilter then.

2 remarks though:
- in the Javadoc of JTextField a similar example is handled by creating a specific PlainDocument. so this should be altered
- if you want to set your FilterDocument, you have to do it like this (imho that's a bit messy):



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