Fixed JTextField Size
Is it possible to make a JTextField only allow 2,3,4,....n characters. That is, I want to restrict the JTextField to some number of characters/numbers?
Is it possible to make a JTextField only allow 2,3,4,....n characters. That is, I want to restrict the JTextField to some number of characters/numbers?
public class JTextFieldLimit extends PlainDocument {
private int limit;
JTextFieldLimit(int limit) {
super();
this.limit = limit;
}
public void insertString
(int offset, String str, AttributeSet attr)
throws BadLocationException {
if (str == null) return;
if ((getLength() + str.length()) <= limit) {
super.insertString(offset, str, attr);
}
}
}
--
http://www.rgagnon.com/howto.html