Granny's Programming Pearls
"inside of every large program is a small program struggling to get out"
JavaRanch.com/granny.jsp
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


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "Resticting Textfield Size" Watch "Resticting Textfield Size" New topic
Author

Resticting Textfield Size

Rajasree R
Greenhorn

Joined: May 23, 2005
Posts: 12
Hi,
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: 15299
    
    6

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
Greenhorn

Joined: May 23, 2005
Posts: 12
Hi,
My problem is not that.Restict the SIZE OF TEXTFIELD.not the number of characters in that..ie,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%)
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: Resticting Textfield Size
 
Similar Threads
Fixed size JTextField
Need to limit the text
Determination of width of TextField
Internationalization and Font size
Text field