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?

[158 byte] By [java4life87a] at [2007-11-27 11:29:00]
# 1

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

RealHowToa at 2007-7-29 16:25:27 > top of Java-index,Java Essentials,Java Programming...
# 2

There's a nifty example code for customizing JTextField in the API documentation of the class.

hiwaa at 2007-7-29 16:25:27 > top of Java-index,Java Essentials,Java Programming...