• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

NX: maximum input length of JTextField

 
shan chen
Ranch Hand
Posts: 70
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 17278
6
IntelliJ IDE Mac Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
shan chen
Ranch Hand
Posts: 70
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Mark.
 
shan chen
Ranch Hand
Posts: 70
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have worked it out. The code is not complicated though. I suggest others to have a try.
shan
 
S. Ganapathy
Ranch Hand
Posts: 194
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 70
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 17278
6
IntelliJ IDE Mac Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
And S. In both solutions you can use the same JTextField to limit the number of characters entered into the field.
Mark
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic