How to set DocumentFilter with input length and range? e.g. 1-3 or 10-80

大城市里の小女人 提交于 2019-11-29 18:06:53

You may want to test this (as I haven't), but the basic idea should get you started.

Also check out Document Filter Examples

As to setting a minimum length, you may want to use a InputVerifier as well

class MyIntFilter extends DocumentFilter {

    private int maxLength = 0;

    public void setMaxLength(int maxLength) {
        this.maxLength = maxLength;
    }

    public int getMaxLength() {
        return maxLength;
    }

    public void insertString(FilterBypass fb, int offset, String string,
                    AttributeSet attr) throws BadLocationException {

        Document doc = fb.getDocument();
        StringBuilder sb = new StringBuilder();

        sb.append(doc.getText(0, doc.getLength()));
        sb.insert(offset, string);

        if (maxLength > 0 && doc.getLength() + string.length() <= maxLength) {
            if (test(sb.toString())) {
                super.insertString(fb, offset, string, attr);
            } else {
                // warn the user and don't allow the insert
            }
        }
    }

    private boolean test(String text) {
        try {
            Integer.parseInt(text);
            return true;
        } catch (NumberFormatException e) {
            return false;
        }
    }

    @Override
    public void replace(FilterBypass fb, int offset, int length, String text,
                    AttributeSet attrs) throws BadLocationException {

        Document doc = fb.getDocument();
        StringBuilder sb = new StringBuilder(2);
        sb.append(doc.getText(0, doc.getLength()));
        sb.replace(offset, offset + length, text);

        if (test(sb.toString())) {
            if (sb.length() > maxLength) {
                length = sb.length() - maxLength;
                if (length > 0) {
                    text = text.substring(0, length);
                    super.replace(fb, offset, length, text, attrs);
                }
            }
        } else {
            // warn the user and don't allow the insert
        }

    }

    @Override
    public void remove(FilterBypass fb, int offset, int length)
                    throws BadLocationException {
        Document doc = fb.getDocument();
        StringBuilder sb = new StringBuilder(2);
        sb.append(doc.getText(0, doc.getLength()));
        //sb.append(doc.getText(0, 2));
        sb.delete(offset, offset + length);

        if (test(sb.toString())) {
            super.remove(fb, offset, length);
        } else {
            // warn the user and don't allow the insert
        }

    }
}

Based on MadProgrammer answer I tested editing his "replace" function in "MyIntFilter" class to:

    @Override
    public void replace(FilterBypass fb, int offset, int length, String text,
                AttributeSet attrs) throws BadLocationException {

    Document doc = fb.getDocument();
    StringBuilder sb = new StringBuilder(2);
    sb.append(doc.getText(0, doc.getLength()));
    sb.replace(offset, offset + length, text);

    if (test(sb.toString())) {
        if (sb.length() < maxLength + 1) { //test string size
            super.replace(fb, offset, length, text, attrs);
            }
        }
    } else {
        // warn the user and don't allow the insert
    }

}

it worked fine for me.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!