简体   繁体   English

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

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

I have a Jtextfield named tPvv, wrote a DocumentFilter for accept only numbers, maximum length of 3.And I have a button edit, when I click that button the entire row loaded in textfield's for editing from the jtable(value in Jtextfield tPvv remains constant). 我有一个名为tPvv的Jtextfield,写了一个DocumentFilter只接受数字,最大长度为3,并且我有一个按钮编辑,当我单击该按钮时,从jtable编辑的文本字段中加载的整个行(Jtextfield tPvv中的值保持不变)。 The Jtextfield which are defined without documentFilter works well(loads values from jtable to the textfields based on row selection). 没有documentFilter定义的Jtextfield效果很好(根据行选择将jtable中的值加载到textfield中)。 Also when I comment the DocumentFilter it works well, but I cannot provide the validation (accept number only and the length of 3). 另外,当我对DocumentFilter进行注释时,它可以很好地工作,但是我无法提供验证(仅接受数字且长度为3)。

I need to check the validation for the tPvv and also load the values from the jtable based on different row selection by clicking the edit button. 我需要检查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));

`last commented line i defined in my code for the validation purpose call. 我在代码中为验证目的调用定义的最后一条注释行。 Please solve this problem. 请解决这个问题。

You're basically ignoring the parameters that are passed to you and what they mean... 您基本上会忽略传递给您的参数及其含义……

  • offset is the offset within the document where the new text will be inserted... offset是将在其中插入新text的文档中的偏移量...
  • length is the number of characters to be deleted length是要删除的字符数

Now, if we use the length within the if statement, we start to see a difference. 现在,如果我们在if语句中使用length ,就会开始看到差异。 Basically, when you call setText , the length will be equal to the number of characters in the text field (as all the text is to be replaced)... 基本上,当您调用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);
    }
}

You're also calling super.insertString(fb, offset, text, attrs); 您还调用了super.insertString(fb, offset, text, attrs); in the replace method, which isn't helpful either... replace方法中,也无济于事...

Update from comments 来自评论的更新

The problem you're facing with setText(null) is to do with the fact that isNumeric in your filter is returning false for null and empty ("") values, but these are actually valid in some contexts... setText(null)面临的问题是过滤器中的isNumeric对于null和empty(“”)值返回false ,但这实际上在某些情况下是有效的...

Now, you could alter the isNumeric method, but that might adversely affect the other methods of the filter, instead, we should be place an additional condition in replace to catch this case and deal with it appropriately... 现在,您可以更改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