I would use javax.swing.text.Document
You can initiate it like:
public static Document createTextDocument(int maxLength){
return new TextDocument(maxLength);
}
...
Document document = createTextDocument(5); // limit to 5 chars
textField1.setDocument(document);
document = createTextDocument(10); // limit to 10 chars
textField2.setDocument(document);
Here is custom TextDocument
class:
public class TextDocument extends PlainDocument{
private static final long serialVersionUID = 1L;
private int maxLength = Integer.MAX_VALUE;
TextDocument(){}
TextDocument(int maxlength){this.maxLength=maxlength;}
public void setMaxLength(int maxLength) {
if (maxLength < 0)
throw new IllegalArgumentException("maxLength<0");
this.maxLength = maxLength;
}
public void insertString(int offset, String str, AttributeSet attr)
throws BadLocationException {
if (validateLength(offset, str) == false)
return;
super.insertString(offset, str, attr);
}
private boolean validateLength(int offset, String toAdd) {
String str_temp;
//String str_text = "";
String str1 = "";
String str2 = "";
try {
str1 = getText(0, offset);
str2 = getText(offset, getLength() - offset);
} catch (Exception e) {
e.printStackTrace();
}
str_temp = str1 + toAdd + str2;
if (maxLength < str_temp.length()) {
beep();
return false;
} else
return true;
}
private void beep() {
java.awt.Toolkit.getDefaultToolkit().beep();
}
}
Hope it will help