我有一个程序,它从JTextField中删除所有非数字字符,并将其限制在5位以内。但是这个文档过滤器也删除了backspace函数,这意味着我无法编辑我所做的输入。如何在不删除筛选器的情况下再次添加回退空间?
编辑:谢谢你的回答。我已经将这个功能添加到“公共无效删除”中,现在我的删除工作又开始了。但我注意到它将我的文本输入向后存储。如果我写"12345“,然后使用我的(int -1),它会删除"1",然后"2”等等。它为什么要这么做?
public class onlyNumericDocumentFilter extends DocumentFilter {
@Override
public void insertString(DocumentFilter.FilterBypass fb, int offset,
String string, AttributeSet attr) throws BadLocationException {
if (fb.getDocument().getLength() + string.length() > 5) {
return;
}
fb.insertString(offset, string, attr);
}
@Override
public void remove(DocumentFilter.FilterBypass fb, int offset, int length)
throws BadLocationException {
//edit:
fb.remove(length-1, 1);
// fb.insertString(offset, "", null);
}
@Override
public void replace(DocumentFilter.FilterBypass fb, int offset, int length,
String text, AttributeSet attr) throws BadLocationException {
if (fb.getDocument().getLength() + text.length() > 5) {
return;
}
fb.insertString(offset, text.replaceAll("\\D", ""), attr);
}
}发布于 2013-12-02 08:57:44
你在这里禁止移除
public void remove(DocumentFilter.FilterBypass fb, int offset, int length) throws BadLocationException
{
fb.insertString(offset, "", null);
} https://stackoverflow.com/questions/20324184
复制相似问题