This week's book giveaway is in the Java 8 forum.
We're giving away four copies of Java 8 in Action and have Raoul-Gabriel Urma, Mario Fusco, and Alan Mycroft 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 Java 8 in Action this week in the Java 8 forum!
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "Document Filter problem" Watch "Document Filter problem" New topic
Author

Document Filter problem

Leo Pereira
Greenhorn

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

http://docs.oracle.com/javase/tutorial/uiswing/examples/components/TextComponentDemoProject/src/components/DocumentSizeFilter.java

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

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

Joined: Jan 25, 2013
Posts: 2
Thank you very much. That seems to have solved the problem.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Document Filter problem
 
Similar Threads
How to show arabic and arabic in a JTextBox when user enter the numbers?
Disappointed with JFormattedTextField
Reg. Validatin JTextFields
setEditable and blinking cursor
How to track backspace key?