Click here to Skip to main content
15,884,473 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I am new to netbeans(7.0.1).Help me to set Max character length in
jTextField
Posted

1 solution

Use PlainDocument to set the max limit. Here is a rough sketch :

Java
public class JTextFieldLimit extends PlainDocument {

  public void insertString( int offset, String  str, AttributeSet attr ) throws BadLocationException {
//put yoyr max length validation logic

super.insertString(offset, str, attr);
  }
}
 
Share this answer
 
v2
Comments
RAMALINGAM.K 25-Jun-13 5:11am    
Actually i want my textbox to allow only 10 numbers.11th number wont get entered there.Is there any property related to that(like maxlength property in .net)but in Jtextbox properties, there is no maxlength property..am using netbeans (7.0.1)
Shubhashish_Mandal 25-Jun-13 5:18am    
Using PlainDocument is the easiest way to do that and its also reusable. However you can add keyListener to read the input ,but its not an acceptable solution.
RAMALINGAM.K 25-Jun-13 5:25am    
how do i achieve this using key listener????
TorstenH. 25-Jun-13 5:29am    
Click me hard

You should bookmark that page and the surrounding tutorials.
RAMALINGAM.K 25-Jun-13 6:25am    
i tried with Plain document but i got error in this line (super.insertString(offs, str, a);)
the error is "no suitable method found for insertString(int,java.lang.String,javax.print.attribute.AttributeSet)
method javax.swing.text.PlainDocument.insertString(int,java.lang.String,javax.swing.text.AttributeSet) is not applicable
(actual argument javax.print.attribute.AttributeSet cannot be converted to javax.swing.text.AttributeSet by method invocation conversion)"

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900