18 lines
511 B
Java
18 lines
511 B
Java
|
import javax.swing.text.PlainDocument;
|
||
|
import javax.swing.text.AttributeSet;
|
||
|
import javax.swing.text.BadLocationException;
|
||
|
|
||
|
public class JTextFieldCharLimit extends PlainDocument{
|
||
|
private int limit;
|
||
|
|
||
|
public JTextFieldCharLimit(int limit){
|
||
|
this.limit = limit;
|
||
|
}
|
||
|
public void insertString(int offset, String str, AttributeSet set) throws BadLocationException{
|
||
|
if (str == null){
|
||
|
return;
|
||
|
} else if ((getLength() + str.length() - getLength()) <= limit){
|
||
|
super.insertString(offset, str, set);
|
||
|
}
|
||
|
}
|
||
|
}
|