aspose file tools*
The moose likes Swing / AWT / SWT and the fly likes JTextField set size limit. Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of EJB 3 in Action this week in the EJB and other Java EE Technologies forum!
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "JTextField set size limit." Watch "JTextField set size limit." New topic
Author

JTextField set size limit.

Maja Gralewska
Ranch Hand

Joined: Sep 26, 2008
Posts: 92
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


Potrzeba jest matka wynalazków
Maneesh Godbole
Saloon Keeper

Joined: Jul 26, 2007
Posts: 9990
    
    7

Check this out
http://www.coderanch.com/t/434129/Swing-AWT-SWT-JFace/java/Reg-Validatin-JTextFields

[How to ask questions] [Donate a pint, save a life!] [Onff-turn it on!]
kati ameri
Greenhorn

Joined: Mar 04, 2009
Posts: 4
Please read this.
http://faq.javaranch.com/java/UseOneThreadPerQuestion

[MG Removed thread hijack]
Maja Gralewska
Ranch Hand

Joined: Sep 26, 2008
Posts: 92
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

Joined: Oct 27, 2005
Posts: 19541
    
  16

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.


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
Maja Gralewska
Ranch Hand

Joined: Sep 26, 2008
Posts: 92


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

Joined: Sep 26, 2008
Posts: 92
Thanks guys I got it



Thanks for all your help.
Maja Gralewska
Ranch Hand

Joined: Sep 26, 2008
Posts: 92
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

Joined: Oct 27, 2005
Posts: 19541
    
  16


Although it can be a bit more neat by putting the "too-long" check first:
Maja Gralewska
Ranch Hand

Joined: Sep 26, 2008
Posts: 92
Thanks
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: JTextField set size limit.
 
Similar Threads
Beginning GUI programming exercise. Please help to figure out why it does not compile.
JTextField/Reading Content
how to create an Array of JTextField !!!!
Runtime Changes to Your GUI
text event