one way u can do like this, The following two classes are to create a textfield with
restricting the number of characters to be entered into it.
=============================================================
public class FixedLengthPlainDocument extends PlainDocument {
private int maxLength;
public FixedLengthPlainDocument(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);
}
}
}
public class FixedLengthTextArea extends JTextArea{
public FixedLengthTextArea (int length)
{
this(null, length);
}
public FixedLengthTextArea (String text, int length) {
super(new FixedLengthPlainDocument(length), text,
length);
}
}
========================================================= so now u can create ur text field as
JTextArea tf = new FixedLengthTextArea (10);
restricting the number of characters to be entered into it.
=============================================================
public class FixedLengthPlainDocument extends PlainDocument {
private int maxLength;
public FixedLengthPlainDocument(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);
}
}
}
public class FixedLengthTextArea extends JTextArea{
public FixedLengthTextArea (int length)
{
this(null, length);
}
public FixedLengthTextArea (String text, int length) {
super(new FixedLengthPlainDocument(length), text,
length);
}
}
========================================================= so now u can create ur text field as
JTextArea tf = new FixedLengthTextArea (10);
解决方案 »
免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货