d2jsp
Log InRegister
d2jsp Forums > Off-Topic > Computers & IT > Programming & Development > Jtextfield
Add Reply New Topic New Poll
Member
Posts: 4,841
Joined: Jan 16 2008
Gold: 0.00
May 17 2013 01:10pm
Is there a way to set a maximum number of characters that can be entered into a JTextfield?
For example, if I initialized a textfield with

Code
new JTextField(5);

I only want the user to be able to enter a maximum of 5 characters.

This post was edited by tt_toby on May 17 2013 01:11pm
Member
Posts: 3,451
Joined: Feb 26 2010
Gold: 0.20
Member
Posts: 1,241
Joined: Jun 25 2011
Gold: Locked
May 17 2013 06:13pm
don't know if you can set it directly, but you could use a CarretListener and trim() the string if needed by intercepting carretUpdate events
Member
Posts: 4,605
Joined: Sep 15 2011
Gold: 9,464.00
May 18 2013 02:59am
ok... questions like these are mindnumbingly stupid.

seriously, don't be programming if you can't even look up javadocs yourself.
Member
Posts: 2,757
Joined: Nov 26 2007
Gold: 1,214.81
May 18 2013 09:22pm
You can override the insertString() function of Document. And if the textField length == the limit, do nothing.

Sorry I can't post an example. I'm writing this post from my iphone. But here's a post that explains it, however you don't need to write a whole new class for it.
stackoverflow.com/questions/3519151/how-to-limit-the-number-of-characters-in-jtextfield
Go Back To Programming & Development Topic List
Add Reply New Topic New Poll