• 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 Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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):



 
reply
    Bookmark Topic Watch Topic
  • New Topic