Source Code : JTextField Max Length
Java Is Open Source Programming Language You Can Download From Java and Java Libraries From http://www.oracle.com.
Click Here to download
We provide this code related to title for you to solve your developing problem easily. Libraries which is import in this program you can download from http://www.oracle.com.
Click Here or search from google with Libraries Name you get jar file related it
JTextField Max Length
import javax.swing.JTextField;
import javax.swing.text.AttributeSet;
import javax.swing.text.BadLocationException;
import javax.swing.text.PlainDocument;
public class JTextFieldMaxLength extends JTextField{
public JTextFieldMaxLength(int length){
this(null,length);
}
public JTextFieldMaxLength(String text, int length){
super(new PlainDocumentMaxLength(length),text,length);
}
}
class PlainDocumentMaxLength extends PlainDocument{
private int maxLength;
public PlainDocumentMaxLength(int maxLength) {
this.maxLength = maxLength;
}
public void insertString (int offset, String str, AttributeSet a)
throws BadLocationException { if (getLength() + str.length() > maxLength) {
// Toolkit.getDefaultToolkit().beep();
}else{
super.insertString(offset,str,a);
}
}
}
Thank with us