File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Swing / AWT / SWT and the fly likes Resticting Textfield Size Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "Resticting Textfield Size" Watch "Resticting Textfield Size" New topic

Resticting Textfield Size

Rajasree R

Joined: May 23, 2005
Posts: 12
I want to have a textfield which accepts limited number of characters.This can be done using JFormattedTextField.But how can I restict the size of the textfield to accept only that many characters.
Gregg Bolinger
GenRocket Founder
Ranch Hand

Joined: Jul 11, 2001
Posts: 15302

Uh, you answered your own question. Say you wanted to limit youself to 4 numbers.

MaskFormatter mf = new MaskFormatter("####");
JFormattedTextField tf = new JFormattedTextField(mf);

GenRocket - Experts at Building Test Data
Craig Wood
Ranch Hand

Joined: Jan 14, 2004
Posts: 1535
Some other options if you find MaskFormatter doesn't meet your needs.
Rajasree R

Joined: May 23, 2005
Posts: 12
My problem is not that.Restict the SIZE OF TEXTFIELD.not the number of characters in,the size(display) of textfield should be resticted accoring to the number of characters..
Swati Udas
Ranch Hand

Joined: Aug 18, 2004
Posts: 121
Well that sounds even simpler..
I guess u can use the right constructor for the textfield

JTextField txt=new JTextField("String",30);

so now u have a textfield which will be shown with size 30 on ur UI..irrespective of the stringlength displayed inside this field.

SCJP 1.4 (90%)<br />SCWCD 1.4 (88%)
I agree. Here's the link:
subject: Resticting Textfield Size
It's not a secret anymore!