Two Laptop Bag*
The moose likes Swing / AWT / SWT and the fly likes InputVerifier, Document or DocumentFilter Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "InputVerifier, Document or DocumentFilter" Watch "InputVerifier, Document or DocumentFilter" New topic
Author

InputVerifier, Document or DocumentFilter

Roel De Nijs
Bartender

Joined: Jul 19, 2004
Posts: 5218
    
  12

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


SCJA, SCJP (1.4 | 5.0 | 6.0), SCJD
http://www.javaroe.be/
Michael Dunn
Ranch Hand

Joined: Jun 09, 2003
Posts: 4632
> "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
Bartender

Joined: Jul 19, 2004
Posts: 5218
    
  12

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):



 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: InputVerifier, Document or DocumentFilter