• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Document Filter problem

 
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Ranch Hand
Posts: 4632
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thank you very much. That seems to have solved the problem.
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic