Limiting how much a user can enter

Reply

Join Date: Apr 2008
Posts: 108
Reputation: christiangirl is an unknown quantity at this point 
Solved Threads: 1
christiangirl christiangirl is offline Offline
Junior Poster

Limiting how much a user can enter

 
0
  #1
May 21st, 2009
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!
Reply With Quote Quick reply to this message  
Join Date: Sep 2008
Posts: 1,566
Reputation: BestJewSinceJC is a name known to all BestJewSinceJC is a name known to all BestJewSinceJC is a name known to all BestJewSinceJC is a name known to all BestJewSinceJC is a name known to all BestJewSinceJC is a name known to all 
Solved Threads: 196
BestJewSinceJC BestJewSinceJC is offline Offline
Posting Virtuoso

Re: Limiting how much a user can enter

 
1
  #2
May 21st, 2009
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.
Reply With Quote Quick reply to this message  
Join Date: Oct 2008
Posts: 2,612
Reputation: adatapost has much to be proud of adatapost has much to be proud of adatapost has much to be proud of adatapost has much to be proud of adatapost has much to be proud of adatapost has much to be proud of adatapost has much to be proud of adatapost has much to be proud of adatapost has much to be proud of adatapost has much to be proud of 
Solved Threads: 461
Moderator
adatapost's Avatar
adatapost adatapost is offline Offline
Posting Maven

Re: Limiting how much a user can enter

 
0
  #3
May 22nd, 2009
Hi Poster,

BestJewSinceJC's explanation is correct. There is no direct method to force user to restrict input.
Failure is not fatal, but failure to change might be. - John Wooden
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC