When I'm trying to use of this DocumentFilter the ctrl+v and ctrl+c is not working.
public class stringLengthTrim extends DocumentFilter {
private int limit;
public stringLengthTrim(int limit) {
this.limit = limit;
}
@Override
public void insertString(FilterBypass fb, int offset, String string, AttributeSet attr) throws BadLocationException {
Document innerDoc = fb.getDocument();
StringBuilder sb = new StringBuilder(innerDoc.getText(0, innerDoc.getLength()));
sb.insert(offset, string);
if (textOK(sb.toString())) {
super.insertString(fb, offset, string, attr);
}
}
@Override
public void replace(FilterBypass fb, int offset, int length, String text, AttributeSet attrs) throws BadLocationException {
Document innerDoc = fb.getDocument();
StringBuilder sb = new StringBuilder(innerDoc.getText(0, innerDoc.getLength()));
int start = offset;
int end = offset + length;
sb.replace(start, end, text);
if (textOK(sb.toString())) {
super.replace(fb, offset, length, text, attrs);
}
}
@Override
public void remove(FilterBypass fb, int offset, int length)
throws BadLocationException {
super.remove(fb, offset, length);
}
private boolean textOK(String text) {
if (text.length() <= limit) {
return true;
}
return false;
}
}
what can i do ? i'm using this documentfilter just for control the length of input text.is another way to restrict the length of JTextarea or JTextfield?
how to set documentfilter to support ctrl+v and ctrl+c?
and this is usage:
PlainDocument cID = (PlainDocument) contentID.getDocument();
cID.setDocumentFilter(new stringLengthTrim(9));
the contentID is a JTextarea.