File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Swing / AWT / SWT and the fly likes Difference between using Document Filter and Plaint Document as input limit length in JTextField?? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of JavaScript Promises Essentials this week in the JavaScript forum!
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "Difference between using Document Filter and Plaint Document as input limit length in JTextField??" Watch "Difference between using Document Filter and Plaint Document as input limit length in JTextField??" New topic
Author

Difference between using Document Filter and Plaint Document as input limit length in JTextField??

ethaniel landfough
Greenhorn

Joined: Aug 16, 2009
Posts: 21
Hi,..
I also post in dream in code about this.DreaminCode.net
In example i found on Java2s Tutorial, it using the plain document as the input limit in JTextField. Is there a difference when using Plain Document or Document Filter ? Anyone can explain , please?
Rob Camick
Ranch Hand

Joined: Jun 13, 2009
Posts: 2213
    
    7
A DocumentFilter is better. You can add it to any Document that extends AbstractDocument. Which means you can use it on any JTextComponent.

If you use a custom Document then you can only use it on a text component that uses that Document.
Brian Cole
Author
Ranch Hand

Joined: Sep 20, 2005
Posts: 862
ethaniel landfough wrote:
I also post in dream in code about this.DreaminCode.net
In example i found on Java2s Tutorial, it using the plain document as the input limit in JTextField. Is there a difference when using Plain Document or Document Filter ? Anyone can explain , please?


One thing that you may not realize is that DocumentFilter was new* with JDK 1.4, which was released in 2002. Many tutorials were written before then, so of course they don't use DocumentFilter.

I would say DocumentFilter (or perhaps JFormattedTextField, which can be cumbersome to use) is the "modern"/"correct" way to do this kind of thing unless you need compatibility with JDK 1.3 or earlier.



footnote:
*This is why the setDocumentFilter() method is not part of the Document interface. Adding it to Document would have broken compatibility, so the next best thing was to add it to AbstractDocument.


bitguru blog
Zhui Shen Ge
Greenhorn

Joined: Nov 22, 2009
Posts: 5
Hi there

I have set a Document to have max 2 chars, but am still able to do something like JTable.setValueAt("very long text")

Is it possible to have the same restriction as the Document when I setValueAt? Much help appreciated here.
Rob Camick
Ranch Hand

Joined: Jun 13, 2009
Posts: 2213
    
    7
Don't hijack older postings.

Your question has nothing to do with the original question.

Posting with two questions and two answers are difficult to follow.
Maneesh Godbole
Saloon Keeper

Joined: Jul 26, 2007
Posts: 10519
    
    9

Zhui,
Please read http://faq.javaranch.com/java/UseOneThreadPerQuestion


[How to ask questions] [Donate a pint, save a life!] [Onff-turn it on!]
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Difference between using Document Filter and Plaint Document as input limit length in JTextField??