This week's giveaway is in the Spring forum.
We're giving away four copies of REST with Spring (video course) and have Eugen Paraschiv on-line!
See this thread for details.
The moose likes Swing / AWT / SWT and the fly likes Document Filter problem Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of REST with Spring (video course) this week in the Spring forum!
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "Document Filter problem" Watch "Document Filter problem" New topic

Document Filter problem

Leo Pereira

Joined: Jan 25, 2013
Posts: 2
I am using a document filter to restrict the number of characters allowed in a JTextfield to 2. It works fine when I try to type in data. Here's the code

My program basically allows the user to cycle through a set of data, which is displayed on the JTextField. The problem is that if the length of the data stored in the field is 2, the textfield is not updated with the data which comes next in the cycle. (I used setText to assign the next value). For example, if the set of data is 2,5,23,28,6 , what i am getting as output is 2,5,5,5,6. I suppose this is because the sum of the lengths of the data already in the JTextField and the new data is >2. How can i get it to work properly? I have a feeling that the solution is pretty simple, but cant quite figure it out.
Michael Dunn
Ranch Hand

Joined: Jun 09, 2003
Posts: 4632
if you loook at this oracle demo

you'll notice, in replace()
if ((fb.getDocument().getLength() + str.length() - length) <= maxCharacters)

they also use super.replace(...), not fb.replace(...)
Leo Pereira

Joined: Jan 25, 2013
Posts: 2
Thank you very much. That seems to have solved the problem.
I agree. Here's the link:
subject: Document Filter problem
jQuery in Action, 3rd edition