繁体   English   中英

jtextfield documentFilter定义后,值不会加载到jtextfield中

[英]jtextfield documentFilter once defined, value not loading in the jtextfield

我有一个名为tPvv的Jtextfield,写了一个DocumentFilter只接受数字,最大长度为3,并且我有一个按钮编辑,当我单击该按钮时,从jtable编辑的文本字段中加载的整个行(Jtextfield tPvv中的值保持不变)。 没有documentFilter定义的Jtextfield效果很好(根据行选择将jtable中的值加载到textfield中)。 另外,当我对DocumentFilter进行注释时,它可以很好地工作,但是我无法提供验证(仅接受数字且长度为3)。

我需要检查tPvv的有效性,还需要通过单击“编辑”按钮基于不同的行选择从jtable加载值。

`class NumericAndLengthFilter extends DocumentFilter {

        /**
         * Number of characters allowed.
         */
        private int length = 0;

        /**
         * Restricts the number of charcacters can be entered by given length.
         * @param length Number of characters allowed.
         */
        public NumericAndLengthFilter(int length) {
            this.length = length;
        }

        @Override
        public void insertString(FilterBypass fb, int offset, String string,
                AttributeSet attr) throws
                BadLocationException {
            if (isNumeric(string)) {
                if (this.length > 0 && fb.getDocument().getLength() + string.
                        length()
                        > this.length) {
                    return;
                }
                super.insertString(fb, offset, string, attr);
            }
        }

        @Override
        public void replace(FilterBypass fb, int offset, int length, String text,
                AttributeSet attrs) throws
                BadLocationException {
            if (isNumeric(text)) {
                if (this.length > 0 && fb.getDocument().getLength() + text.
                        length()
                        > this.length) {
                    return;
                }
                super.insertString(fb, offset, text, attrs);
            }
        }

        /**
         * This method tests whether given text can be represented as number.
         * This method can be enhanced further for specific needs.
         * @param text Input text.
         * @return {@code true} if given string can be converted to number; otherwise returns {@code false}.
         */
        private boolean isNumeric(String text) {
            if (text == null || text.trim().equals("")) {
                return false;
            }
            for (int iCount = 0; iCount < text.length(); iCount++) {
                if (!Character.isDigit(text.charAt(iCount))) {
                    return false;
                }
            }
            return true;
        }

}
//((AbstractDocument) tPvv.getDocument()).setDocumentFilter(new NumericAndLengthFilter(3));

我在代码中为验证目的调用定义的最后一条注释行。 请解决这个问题。

您基本上会忽略传递给您的参数及其含义……

  • offset是将在其中插入新text的文档中的偏移量...
  • length是要删除的字符数

现在,如果我们在if语句中使用length ,就会开始看到差异。 基本上,当您调用setTextlength将等于文本字段中的字符数(因为所有文本都将被替换)...

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

        System.out.println(offset + "; " + length + "; " + text);

        if (this.length > 0 && 
            fb.getDocument().getLength() + text.length() - length > this.length) {
            return;
        }
        super.replace(fb, offset, length, text, attrs);
    }
}

您还调用了super.insertString(fb, offset, text, attrs); replace方法中,也无济于事...

来自评论的更新

setText(null)面临的问题是过滤器中的isNumeric对于null和empty(“”)值返回false ,但这实际上在某些情况下是有效的...

现在,您可以更改isNumeric方法,但这可能会对过滤器的其他方法产生不利影响,相反,我们应该在replace放置一个附加条件以捕获这种情况并适当地对其进行处理...

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

        System.out.println(offset + "; " + length + "; " + text);

        if (this.length > 0 && 
            fb.getDocument().getLength() + text.length() - length > this.length) {
            return;
        }
        super.replace(fb, offset, length, text, attrs);
    } else if (text == null || text.equals("")) {
        super.replace(fb, offset, length, text, attrs);
    }
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM