• 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
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

limit number of characters in a JTextField?

 
Ranch Hand
Posts: 455
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I'm using VA for Java 3.02 and of course, I am a true beginner. I've kind of been playing around with it for the last week. My problem is I want to limit the number of characters that a user can key in a text field. I can't seem to figure out how to do this. Can someone please help me? I would be forever grateful.
Thanks!
 
Ranch Hand
Posts: 216
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
That is really IDE independent so this might not be the best forum to post Your question in.
BTW you can either use a constructor JTextField(int columns) which constructs a new empty TextField with the specified number of columns or u can use setColumns(int columns) method that sets the number of columns in this TextField.
Vladan
 
Jennifer Sohl
Ranch Hand
Posts: 455
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello. Thanks for reply. Sorry about posting this in the wrong forum.
I tried your suggestions and it did change the number of columns in my text field. However, it still let me key as many characters as I wanted. (Ex. specified 15 columns, let me key as many characters as I wanted. It just kept scrolling the text over. I want to be able to key ONLY 15 characters).
Any other suggestions.
Your help is very appreciated!
 
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You have to use PlainDocument Model for this purpose..
 
Bartender
Posts: 783
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I'm going to move this to the more appropriate forum, AWT/Swing.
-Peter
 
Ranch Hand
Posts: 347
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Jennifer,
One way to achieve your goal is to extend JTextField to only accept a certain number of characters. An example class to use is below:

Hope this helps.
Stephanie
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic