• 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

JTextField set size limit.

 
Ranch Hand
Posts: 92
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
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
Check this out
https://coderanch.com/t/434129/Swing-AWT-SWT-JFace/java/Reg-Validatin-JTextFields
 
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Sheriff
Posts: 22783
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
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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Posts: 22783
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

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
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks:)
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic