Archived

This topic is now archived and is closed to further replies.

[java] Limit length of strings/input in edit box

This topic is 5116 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

You would have to subclass JTextField and overrride processInputEvents and call enableEvents. Then you should be able to catch each key typed to count to your limit. Then keep any more text from being entered if your limit is reached.



First make it work, then make it fast. --Brian Kernighan

The problems of this world cannot possibly be solved by skeptics or cynics whose horizons are limited by the obvious realities. We need men and women who can dream of things that never were. - John Fitzgerald Kennedy(35th US President)

Do not interrupt your enemy when he is making a mistake. - Napolean Bonaparte

Share this post


Link to post
Share on other sites
If that''s too difficult, you could always just allow the user to enter as long a string as they wanted to, but only take the first 16 characters of the string. Of course, you should put a warning, telling them about the 16 character limit.

To get the first 16 characters, use the substring(0,16) method of the String class.

Share this post


Link to post
Share on other sites