-1

tPvv という名前の Jtextfield があり、最大長 3 の数字のみを受け入れる DocumentFilter を作成しました。また、編集ボタンがあります。そのボタンをクリックすると、jtable から編集するために textfield にロードされた行全体がロードされます (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));

`検証目的の呼び出しのためにコードで定義した最後のコメント行。この問題を解決してください。

4

1 に答える 1

3

基本的に、渡されるパラメーターとその意味を無視しています...

  • offsettext新しいものが挿入されるドキュメント内のオフセットです...
  • lengthは削除する文字数です

ここで、ステートメントlength内でを使用すると、違いが見え始めます。if基本的に、 を呼び出すと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いますが、これも役に立ちません...

コメントから更新

あなたが直面している問題は、フィルターがforと空 ("") の値を返すsetText(null)という事実に関係していますが、これらは実際にはいくつかのコンテキストで有効です...isNumericfalsenull

メソッドを変更できますが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);
    }
}
于 2013-09-04T09:25:40.777 に答える