Win a copy of Functional Reactive Programming this week in the Other Languages forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

JTextField set size limit.

 
Maja Gralewska
Ranch Hand
Posts: 92
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

Whats the simplest way i can set textfield to have no more then 12 charecters so that when someone enteres the 12th and tries to type the 13th, it will not type. I do not want to create a listener(preferably a method not a clas that has to be extended) but rather use some kind of setSize or setMaxLimit or something...but i do not see anything that is applicable..(preferable something compatible with the older types of java aswell).



Thanks
 
kati ameri
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Please read this.
http://faq.javaranch.com/java/UseOneThreadPerQuestion

[MG Removed thread hijack]
 
Maja Gralewska
Ranch Hand
Posts: 92
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

Im trying to add this action listener to all these jText fields one by one, but it only adds it to one of the jTextFields and ignores all the other ones in the array. Any ideas on how to fix this problem?



Thanks for all your help.
 
Rob Spoor
Sheriff
Pie
Posts: 20667
65
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Using a key listener is not a good idea for limiting the size, since it does not handle paste actions well. With a Document, as in the other thread Maneesh linked to, the insertString method gets everything that would be added - not just key presses, but also paste actions. It also works when calling setText on the field.
 
Maja Gralewska
Ranch Hand
Posts: 92
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


I tried this but it doesn't work at all. I'm very confused with this class, can anybody tell me what i'm doing wrong? Isn't there a simpler way to set the limit on a textfield?


Thanks.
 
Maja Gralewska
Ranch Hand
Posts: 92
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks guys I got it



Thanks for all your help.
 
Maja Gralewska
Ranch Hand
Posts: 92
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

The code above sets the limit and i can past as long as the amount of characters is less then the limit, but as soon as i try to paste anything over the limit it refuses to paste(blank), is there a way to make it past but only to the limit?

Thanks
 
Rob Spoor
Sheriff
Pie
Posts: 20667
65
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Although it can be a bit more neat by putting the "too-long" check first:
 
Maja Gralewska
Ranch Hand
Posts: 92
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic