• 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

Limit the character in JTextField

 
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I also post my problem in dreamincode.net. Is there a way to limit the character in JTextField?
Because all i have found is limit number of input in JTextField which make me can't replace the text or even use backspace..
What i need is i can input only 1 character but i can use backspace to make it empty again and input again the new character ? Is it possible?? Thanks...

(I add it like this..)
 
Bartender
Posts: 11497
19
Android Google Web Toolkit Mac Eclipse IDE Ubuntu Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Swing related.
Moving.
 
Sheriff
Posts: 22781
131
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Read this for how to use DocumentFilters. Your filter's replace method should simply ensure that the document length is either 0 or 1.
 
ethaniel landfough
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Rob Prime wrote:Read this for how to use DocumentFilters. Your filter's replace method should simply ensure that the document length is either 0 or 1.


Thanks, i have read about it..
Someone in dreamincode.net giving me answer like this :

Try using a key listener to detect each time the user enters text into the field. Each time the key event is triggered, get the length() of the text in the JTextField. If the length >= limit, then disable editing. However, if the delete key is pressed, remove the last character in the JTextField and setEditable(true). Hope this helps some. Good luck!



Is this possible?

Because i don't know much about DocumentFilter, i would rather prefer a simple way if there's still a choice.

 
Ranch Hand
Posts: 624
IntelliJ IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The suggestion you received on dreamincode.net is certainly doable. But it is, in my humble opinion, a less elegant solution than the DocumentFilter solution and can be more problematic. I've seen solutions like that break under extreme circumstances (think a 5-year old banging on a keyboard.)

The DocumentFilter exists to solve the very problem you want to solve. As such, it is probably the solution you should use.

You can see an example of a DocumentFilter being used to solve the very problem you are trying to solve at exampledepot.com: Limiting the Capacity of a JTextComponent

When trying to solve problems like this, my recommendation and advice is not to shy away from solutions that use something "that [you] don't know much about". You will have to work with things you do not know much about all the time in programming. It is the nature of the beast. Learning how to assimilate to something new is a valuable skill. And the more you do it, the better you become at it. Just some advice that I think will help you become a better programmer.
 
ethaniel landfough
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Mark Vedder wrote:The suggestion you received on dreamincode.net is certainly doable. But it is, in my humble opinion, a less elegant solution than the DocumentFilter solution and can be more problematic. I've seen solutions like that break under extreme circumstances (think a 5-year old banging on a keyboard.)

The DocumentFilter exists to solve the very problem you want to solve. As such, it is probably the solution you should use.

You can see an example of a DocumentFilter being used to solve the very problem you are trying to solve at exampledepot.com: Limiting the Capacity of a JTextComponent

When trying to solve problems like this, my recommendation and advice is not to shy away from solutions that use something "that [you] don't know much about". You will have to work with things you do not know much about all the time in programming. It is the nature of the beast. Learning how to assimilate to something new is a valuable skill. And the more you do it, the better you become at it. Just some advice that I think will help you become a better programmer.



Ok, thanks a lot,Mark. I'll try my best to learn.
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic