Win a copy of The Java Performance Companion this week in the Performance forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

limit number of characters in a JTextField?

 
Jennifer Sohl
Ranch Hand
Posts: 455
  • Mark post as helpful
  • send pies
  • 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!
 
Vladan Radovanovic
Ranch Hand
Posts: 216
  • Mark post as helpful
  • send pies
  • 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
  • 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!
 
prasadsvja
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You have to use PlainDocument Model for this purpose..
 
Peter Tran
Bartender
Posts: 783
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm going to move this to the more appropriate forum, AWT/Swing.
-Peter
 
Stephanie Grasson
Ranch Hand
Posts: 347
  • Mark post as helpful
  • send pies
  • 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
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic