• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

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

 
ethaniel landfough
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 2619
9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 908
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Zhui Shen Ge
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 2619
9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 11196
15
Android Eclipse IDE Google Web Toolkit Java Mac Ubuntu
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic