aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes set maximum input length of JTextField Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "set maximum input length of JTextField" Watch "set maximum input length of JTextField" New topic
Author

set maximum input length of JTextField

shan chen
Ranch Hand

Joined: Apr 09, 2003
Posts: 70
Hi, all,

How can we set the maximum input string length of JTextField? I hope that user can type in at most 10 characters in the JTextField.
JTextFiled t1 = new JTextField(10);
t1.setColumns(10);
But when the application runs, user can still type in more than 10 characters. What should I do?
Thanks to any suggestions.
Andrew Monkhouse
author and jackaroo
Marshal Commander

Joined: Mar 28, 2003
Posts: 11503
    
  95

Bartenders: Shan Chen is currently doing the SCJD assignment, and this question looks like it is related to that assignment ... do you want to move this thread?
Hi Shan,
You may be able to do this by extending JTextField and extending PlainDocument to set a custom editor. There is a tutorial on this somewhere, let me see....
Take a look at Sun tutorial on creating text fields ... the section on creating a validated text field.
Regards, Andrew


The Sun Certified Java Developer Exam with J2SE 5: paper version from Amazon, PDF from Apress, Online reference: Books 24x7 Personal blog
shan chen
Ranch Hand

Joined: Apr 09, 2003
Posts: 70
Hi, Andrew,
Thanks for your help.
A validated textfield is to check the input after user has typed in something. What I wish to get is that user can not type in any character after 10 characters. Like 'input' in html:
<input name="tf" value="tf" maxlength=10 ... >
I will read the paper you mentioned anyway.
Thanks.
Shan
Andrew Monkhouse
author and jackaroo
Marshal Commander

Joined: Mar 28, 2003
Posts: 11503
    
  95

Hi Shan,
What I suggested can be used to validate on the fly.
The data in a change-validated field is checked each time the field changes. A field that is change-validated can never contain invalid data because every change (keystroke, cut, copy, and so on) that might cause the data to be invalid is rejected.

I have used this to create a field in my application that will only allow numeric inputs. It refuses to allow any other character to be entered.
From memory of how this works, I think you should be able to put a size limit on the field as well.
Regards, Andrew
[ May 19, 2003: Message edited by: Andrew Monkhouse ]
shan chen
Ranch Hand

Joined: Apr 09, 2003
Posts: 70
Hi, Andrew,
The method you mentioned should be able to slove the problem. But the code is a little bit complicated. I have posted the same topic in SCJD forum to see if there is any simplier way, or maybe no other ways.
Thank you.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: set maximum input length of JTextField