• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • paul wheaton
  • Liutauras Vilda
  • Ron McLeod
Sheriffs:
  • Jeanne Boyarsky
  • Devaka Cooray
  • Paul Clapham
Saloon Keepers:
  • Scott Selikoff
  • Tim Holloway
  • Piet Souris
  • Mikalai Zaikin
  • Frits Walraven
Bartenders:
  • Stephan van Hulst
  • Carey Brown

NX: maximum input length of JTextField

 
Ranch Hand
Posts: 70
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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:
https://coderanch.com/t/241875/java-programmer-SCJP/certification/set-maximum-input-length-JTextField
Thanks to any suggestion.
 
ranger
Posts: 17347
11
Mac IntelliJ IDE Spring
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks Mark.
 
shan chen
Ranch Hand
Posts: 70
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I have worked it out. The code is not complicated though. I suggest others to have a try.
shan
 
Ranch Hand
Posts: 194
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Posts: 17347
11
Mac IntelliJ IDE Spring
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Politics n. Poly "many" + ticks "blood sucking insects". Tiny ad:
Gift giving made easy with the permaculture playing cards
https://coderanch.com/t/777758/Gift-giving-easy-permaculture-playing
reply
    Bookmark Topic Watch Topic
  • New Topic