> How does one specify the maximum number of characters that may be entered
> in a TextField or JTextField?
>
> Gregg
You'll need to implement a custom Document that refuses to accept any more
input once the max length has been entered (this only works for Swing
though).
Something like this would do:
public class LimitedLengthDocument extends javax.swing.text.PlainDocument {
private int maxLength;
/** Creates a new instance of LimitedLengthDocument */
public LimitedLengthDocument( int maxLength) {
this.maxLength =maxLength;
}
public LimitedLengthDocument() {
this.maxLength = 3;
}
void setMaxLength(int maxLength) {
this.maxLength = maxLength;
}
public void insertString(int offset, String str, AttributeSet a) throws
BadLocationException {
if ((getLength() + str.length()) <= maxLength) {
super.insertString(offset, str, a);
}else{
java.awt.Toolkit.getDefaultToolkit().beep();
}
}
}
You'll need to give an instance of this document to your JTextField.

Signature
Regards,
Christophe Vanfleteren
Hi Gregg,
Another way to do this...
> How does one specify the maximum number of characters that may be entered in
> a TextField or JTextField?
...if you are using Java 1.4+ is with a JFormattedTextField. You can
do all sorts of cool input validation with it.
--g