0

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!

3
Contributors
2
Replies
3
Views
8 Years
Discussion Span
Last Post by __avd
1

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/tutorial/uiswing/components/textfield.html

Votes + Comments
Good suggestion.
0

Hi Poster,

BestJewSinceJC's explanation is correct. There is no direct method to force user to restrict input.

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.