• Post Reply Bookmark Topic Watch Topic
  • New Topic

Limit the character in JTextField

 
ethaniel landfough
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
  • 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..)
 
Maneesh Godbole
Bartender
Posts: 11445
18
Android Eclipse IDE Google Web Toolkit Java Mac Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Swing related.
Moving.
 
Rob Spoor
Sheriff
Posts: 20822
68
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • 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
  • 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.

 
Mark Vedder
Ranch Hand
Posts: 624
IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • 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
  • 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.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!