Win a copy of The Java Performance Companion this week in the Performance forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Document Filter problem

 
Leo Pereira
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 4632
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you very much. That seems to have solved the problem.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic