943,675 Members | Top Members by Rank

Ad:
  • Java Discussion Thread
  • Unsolved
  • Views: 330
  • Java RSS
May 21st, 2009
0

Limiting how much a user can enter

Expand Post »
Hey,
I am trying to find a way to limit how many characters a user can enter into a JTextField.
Does anyone know how to do this?
Thanks!
Similar Threads
Reputation Points: 20
Solved Threads: 1
Junior Poster
christiangirl is offline Offline
108 posts
since Apr 2008
May 21st, 2009
1

Re: Limiting how much a user can enter

Personally, my first instinct would be to write a listener method that got the text, counted how many characters were there, and "refused to let more be entered" by setting the text back to whatever was previously there if the user entered too much. That would work, however, my first instinct would probably be a waste of time, because there are other available methods of doing the same with less effort and probably less code.


At this link, it describes that you should either use a JFormattedTextField (you're using a JTextField) or you should use a Document listener. It gives links to both. If you want to use a JTextField, stick with the document listener.
http://java.sun.com/docs/books/tutor...textfield.html
Last edited by BestJewSinceJC; May 21st, 2009 at 9:14 pm.
Reputation Points: 874
Solved Threads: 352
Posting Maven
BestJewSinceJC is offline Offline
2,758 posts
since Sep 2008
May 22nd, 2009
0

Re: Limiting how much a user can enter

Hi Poster,

BestJewSinceJC's explanation is correct. There is no direct method to force user to restrict input.
Moderator
Reputation Points: 2136
Solved Threads: 1228
Posting Genius
adatapost is offline Offline
6,527 posts
since Oct 2008

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in Java Forum Timeline: problem with indexOf() and contains()
Next Thread in Java Forum Timeline: Selection Is Not Within A Valid Module





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC