wood burning stoves 2.0*
The moose likes Swing / AWT / SWT and the fly likes limit number of characters in a JTextField? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "limit number of characters in a JTextField?" Watch "limit number of characters in a JTextField?" New topic
Author

limit number of characters in a JTextField?

Jennifer Sohl
Ranch Hand

Joined: Feb 28, 2001
Posts: 455
I'm using VA for Java 3.02 and of course, I am a true beginner. I've kind of been playing around with it for the last week. My problem is I want to limit the number of characters that a user can key in a text field. I can't seem to figure out how to do this. Can someone please help me? I would be forever grateful.
Thanks!
Vladan Radovanovic
Ranch Hand

Joined: Mar 20, 2000
Posts: 216
That is really IDE independent so this might not be the best forum to post Your question in.
BTW you can either use a constructor JTextField(int columns) which constructs a new empty TextField with the specified number of columns or u can use setColumns(int columns) method that sets the number of columns in this TextField.
Vladan
Jennifer Sohl
Ranch Hand

Joined: Feb 28, 2001
Posts: 455
Hello. Thanks for reply. Sorry about posting this in the wrong forum.
I tried your suggestions and it did change the number of columns in my text field. However, it still let me key as many characters as I wanted. (Ex. specified 15 columns, let me key as many characters as I wanted. It just kept scrolling the text over. I want to be able to key ONLY 15 characters).
Any other suggestions.
Your help is very appreciated!
prasadsvja
Greenhorn

Joined: Mar 26, 2001
Posts: 3
You have to use PlainDocument Model for this purpose..
Peter Tran
Bartender

Joined: Jan 02, 2001
Posts: 783
I'm going to move this to the more appropriate forum, AWT/Swing.
-Peter
Stephanie Grasson
Ranch Hand

Joined: Jun 14, 2000
Posts: 347
Jennifer,
One way to achieve your goal is to extend JTextField to only accept a certain number of characters. An example class to use is below:

Hope this helps.
Stephanie
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: limit number of characters in a JTextField?