File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Developer Certification (SCJD/OCMJD) and the fly likes NX: 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 » Developer Certification (SCJD/OCMJD)
Bookmark "NX: maximum input length of JTextField" Watch "NX: maximum input length of JTextField" New topic
Author

NX: maximum input length of JTextField

shan chen
Ranch Hand

Joined: Apr 09, 2003
Posts: 70
Hi,
I have posted this message on SCJP and only Andrew gave me a reply. Thanks to Andrew. I would like to post int here to see if there are any more suggestions.
To book a room, user should enter an 8-digit number as client ID. How to set the maximum input length of the JTextField, so that user can not enter any more digits after 8 digits? The following code does not work.
JTextFiled t1 = new JTextField(8);
t1.setColumns(8);
The origianl thread is at:
http://www.coderanch.com/t/241875/java-programmer-SCJP/certification/set-maximum-input-length-JTextField
Thanks to any suggestion.
Mark Spritzler
ranger
Sheriff

Joined: Feb 05, 2001
Posts: 17259
    
    6

Well, Andrew's answer is the best answer. Making a PlainDocument that accepts only numbers and only up to a certain number of numbers, then PlainDocuments and extending JTextField is the way to go.
Andrew posted a good tutorial, and if I was at my desk at work and not onsite at one of our regions, I'd post the simple code that you need. But I guess it is better this way because you can learn to use PlainDocuments on your own.
Mark


Perfect World Programming, LLC - Two Laptop Bag - Tube Organizer
How to Ask Questions the Smart Way FAQ
shan chen
Ranch Hand

Joined: Apr 09, 2003
Posts: 70
Thanks Mark.
shan chen
Ranch Hand

Joined: Apr 09, 2003
Posts: 70
I have worked it out. The code is not complicated though. I suggest others to have a try.
shan
S. Ganapathy
Ranch Hand

Joined: Mar 26, 2003
Posts: 194
Is it necessary to use text field to book the record? can't we use the editable column once user double clicks on the record to book it, and make only the owner column editable. is this bad idea?
shan chen
Ranch Hand

Joined: Apr 09, 2003
Posts: 70
Hi, Ganapathy,
I think it is fine to use editable column. The question I raised is how to ensure the input is an 8-digit number. For example, "shan1111", "123456789" are incorrect and should be rejected. The progam can check it after user presses "book" button, or does not allow such input on fly.
shan
Mark Spritzler
ranger
Sheriff

Joined: Feb 05, 2001
Posts: 17259
    
    6

And S. In both solutions you can use the same JTextField to limit the number of characters entered into the field.
Mark
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: NX: maximum input length of JTextField