Win a copy of Cross-Platform Desktop Applications: Using Node, Electron, and NW.js this week in the JavaScript forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Using DocumentFilter  RSS feed

 
Prithiraj Sen Gupta
Ranch Hand
Posts: 92
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

I am doing an introductory Project using NetBeans IDE in Java Swing. I have implemented KeyListener interface for validation in a form so that the Numeric data will not get inside a jTextField.


My codes look like the above format but the TextField also takes the numeric keys. I would like my codes to work like this(an Example in JavaScript) for a text Field so that I dont have to popup messages for validation.

Is that possible to make a class for the validation which will work as I have discussed above. so that one don't have to override KeyPressed event again and again with new forms and new controls.

Thanks & Regards

Prithiraj Sen Gupta
[ January 15, 2008: Message edited by: Prithiraj Sen Gupta ]
 
Brian Cole
Author
Ranch Hand
Posts: 947
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
There are a bunch of ways to do this, but I don't see how adding a KeyListener would be much help.

If you can presume 1.4 or better, probably easiest is to use a DocumentFilter:



This is a slightly modified example from my book.
 
Gregg Bolinger
Ranch Hand
Posts: 15304
6
Chrome IntelliJ IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Brian Cole:
This is a slightly modified example from my book.


Brian, I gave you Author status.
 
Prithiraj Sen Gupta
Ranch Hand
Posts: 92
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello Brian Cole,

Thanks for the post on response to the query. I followed your codes and got the successful result.

Thanks & Regards

Prithiraj Sen Gupta
 
Prithiraj Sen Gupta
Ranch Hand
Posts: 92
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello,

I am a new programmer and just started doing an introductory project(as told earlier). The question I am going to ask is little personal. But It will help the novice like me. The codes which you have posted above are really cool and beneficial to anyone.

But I would like to know how a developer or expert like you knows what comes after what. e.g In the above codes you have typed .getDocument after ((AbstracDocument))jta and then set the Document filter by assigning .setDocument to it. Following that you have built a new class which extends DocumentFilter. How do you know all this? And then you have overriden the insertString() and replace() methods. And the thing which has taken my attention mostly is that How do you Know that DocumentFilter class already has these methods which need to override? I am doing the project in NetBeans IDE and already downloaded the jdk-6-doc(Jdk Documentation) from sun microsystems. Basically I want to know the approach one should have when learning Java. What are the points one should focus at first? What is your point of view? And most Importantly How you relate the things(Like classes interfaces, objects etc etc)?

I know with practice one gains the Experience. And with Experience one have the Knowledge. And I will be glad to know How To Practice first then 2nd then 3rd... so on and on..

Thanks & Regards

Prithiraj Sen Gupta
[ December 28, 2007: Message edited by: Prithiraj Sen Gupta ]
 
Campbell Ritchie
Sheriff
Posts: 55324
157
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Prithiraj Sen Gupta:
Hello,

I am a new programmer . . . I would like to know how a developer or expert like you knows what comes after what. e.g In the above codes you have typed .getDocument after ((AbstracDocument))jta . . .


Sounds to me like a question for the beginners' forum, and completely different from the subject of this thread. Maybe you would do well to start a new thread in Java in General (Beginners).
 
Brian Cole
Author
Ranch Hand
Posts: 947
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Prithiraj Sen Gupta:
I would like to know how a developer or expert like you knows what comes after what. e.g In the above codes you have typed .getDocument after ((AbstracDocument))jta and then set the Document filter by assigning .setDocument to it. Following that you have built a new class which extends DocumentFilter. How do you know all this?


That's a reasonable question. The reason I know about DocumentFilter is because I read the Swing Changes and New Features section of the JDK 1.4 release notes back in 2001 or 2002. (DocumentFilter doesn't get its own section, but is described in the JFormattedTextField section.)

The setDocumentFilter() method should really be part of the Document interface, but they couldn't add it for backward compatibility reasons (doing so would have broken all third-party code that happened to implement Document) so they did the next best thing by adding it to AbstractDocument.

This is why the cast to AbstractDocument is necessary and this is why it is hard for novices like you to discover that DocumentFilter exists. You are unlikely to encounter it unless you're scanning the javadoc for AbstractDocument for useful methods, and you're unlikely to be doing that unless you have discovered that (unless you specify otherwise) the Document returned by JTextField.getDocument() is an AbstractDocument. (Actually it's a PlainDocument, as documented here.)

And then you have overriden the insertString() and replace() methods. And the thing which has taken my attention mostly is that How do you Know that DocumentFilter class already has these methods which need to override?


I know the methods exist because they are listed in the javadoc, and also places like that page I linked above. And any non-static public or protected method may be overridden.

How did I know which methods needed to be overridden to obtain your goal? It can be tricky to know in general, but DocumentFilter is a fairly simple case because it's one of those classes (like MouseAdapter) that exists only for subclasses to override. There's no point in ever using a DocumentFilter (or MouseAdapter) without overriding method(s) of interest.
[ December 28, 2007: Message edited by: Brian Cole ]
 
Prithiraj Sen Gupta
Ranch Hand
Posts: 92
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Happy New Year!!!

Thanks for the post.. It really helped me alot..
 
Prithiraj Sen Gupta
Ranch Hand
Posts: 92
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello,

The above codes really helped me. My Application do have many forms with lots of JTextFields/JFormattedTextFields. I am using NetBeans. Here the autogenerated code for the declarations looks like:

When I call the NonNumericFilter/NumericFilter and then set the document filter The above declaration need static keyword. So I have to go to the files(.java) stored in my project folder and have to change the self generated declaration by inserting "static" keyword. Why the declarations for control need static Keyword for setting DocumentFilter? Is there a wayout so that I dont have to get inside these folders and change the self generated codes inside .java files.

Thanks & Regards

Prithiraj
[ January 10, 2008: Message edited by: Prithiraj Sen Gupta ]
 
pete stein
Bartender
Posts: 1561
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Prithiraj Sen Gupta:
[QB]When I call the NonNumericFilter/NumericFilter and then set the document filter The above declaration need static keyword. So I have to go to the files(.java) stored in my project folder and have to change the self generated declaration by inserting "static" keyword. Why the declarations for control need static Keyword for setting DocumentFilter? Is there a wayout so that I dont have to get inside these folders and change the self generated codes inside .java files.


This is happening because you are trying to make changes to the JTextFields in a static non-OOP way, probably within a static method (I'm guessing that you're trying to do this from within the main method). Rather, you need to make the changes to the class's instance variables which is probably best done from within an instance (non-static) method. For some information on instance vs. static members, please have a look at the Sun Java tutorials starting here.

If this doesn't help, then you may want to post some pertinent code that we can look over. Much luck!
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!