Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/batch-file/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java jtextfield documentFilter一旦定义,值不会加载到jtextfield中_Java_Swing_Documentlistener_Documentfilter - Fatal编程技术网

Java jtextfield documentFilter一旦定义,值不会加载到jtextfield中

Java jtextfield documentFilter一旦定义,值不会加载到jtextfield中,java,swing,documentlistener,documentfilter,Java,Swing,Documentlistener,Documentfilter,我有一个名为tPvv的Jtextfield,为只接受数字编写了一个DocumentFilter,最大长度为3。我有一个按钮edit,当我单击该按钮时,在textfield中加载的整行将从jtable进行编辑(Jtextfield tPvv中的值保持不变)。未使用documentFilter定义的Jtextfield工作良好(根据行选择将值从jtable加载到textfields)。此外,当我对DocumentFilter进行注释时,它工作得很好,但我无法提供验证(仅接受数字和长度3) 我需要检查

我有一个名为tPvv的Jtextfield,为只接受数字编写了一个DocumentFilter,最大长度为3。我有一个按钮edit,当我单击该按钮时,在textfield中加载的整行将从jtable进行编辑(Jtextfield tPvv中的值保持不变)。未使用documentFilter定义的Jtextfield工作良好(根据行选择将值从jtable加载到textfields)。此外,当我对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));
`class NumericAndLengthFilter扩展了DocumentFilter{
/**
*允许的字符数。
*/
私有整数长度=0;
/**
*限制按给定长度输入的字符数。
*@param-length允许的字符数。
*/
公共数字长度筛选器(整数长度){
这个长度=长度;
}
@凌驾
public void insertString(FilterBypass fb、int offset、String String、,
属性集属性(attr)抛出
错误位置异常{
如果(是数字(字符串)){
如果(this.length>0&&fb.getDocument().getLength()+字符串。
长度()
>这个(长度){
回来
}
super.insertString(fb,offset,string,attr);
}
}
@凌驾
public void replace(FilterBypass fb、整数偏移量、整数长度、字符串文本、,
属性集属性(attrs)抛出
错误位置异常{
如果(是数字(文本)){
如果(this.length>0&&fb.getDocument().getLength()+文本。
长度()
>这个(长度){
回来
}
super.insertString(fb、偏移量、文本、属性);
}
}
/**
*此方法测试给定文本是否可以表示为数字。
*此方法可根据具体需要进一步增强。
*@param text输入文本。
*@return{@code true}如果给定的字符串可以转换为数字,则返回{@code false}。
*/
专用布尔值isNumeric(字符串文本){
if(text==null | | text.trim().equals(“”){
返回false;
}
对于(int-iCount=0;iCount

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

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

  • 偏移量
    是文档中插入新
    文本
    的偏移量
  • length
    是要删除的字符数
现在,如果我们在
if
语句中使用
length
,我们开始看到差异。基本上,当您调用
setText
时,
length
将等于文本字段中的字符数(因为所有文本都将被替换)

您还调用了super.insertString(fb、offset、text、attrs)replace方法中的code>,这也没有帮助

根据评论更新

setText(null)
所面临的问题与过滤器中的
isNumeric
返回
false
null
和空(“”)值有关,但这些值在某些上下文中实际上是有效的

现在,您可以更改
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);
    }
}

上述代码解决了上述问题,但出现了新的问题,当我单击“添加”按钮时,所有文本字段都变为空或空白(我称为Textfield.setText(null)方法),但tPvv(为此JTextField创建的Docfilter)Textfield组件中的值未被删除。“我称为Textfield.setText(null)”,但是在
isNumeric
中,对于等于
null
且为空的值,返回
false
。我会考虑返回<代码>真< /COD>,但是您需要在您的其他方法中为<代码> NUL/<代码>代码>字符串 S添加额外的检查。或者,向您添加
else if
语句
replace
方法以包含
null
字符串的可能性,并调用
super。对于仅接受数字的文本字段(即使长度有限),您还可以查看
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);
    }
}