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));
`検証目的の呼び出しのためにコードで定義した最後のコメント行。この問題を解決してください。