У меня есть Jtextfield с именем tPvv, написано DocumentFilter для принятия только чисел, максимальная длина 3. И у меня есть кнопка редактирования, когда я нажимаю эту кнопку, вся загруженная строка в текстовом поле для редактирования из jtable (значение в Jtextfield tPvv остается постоянным). Jtextfield, которые определены без documentFilter, хорошо работает (загружает значения из jtable в текстовые поля на основе выбора строки). Также, когда я комментирую DocumentFilter, он работает хорошо, но я не могу предоставить подтверждение (принять только номер и длину 3).jtextfield documentFilter однажды определено, значение не загружается в jtextfield
Мне нужно проверить валидацию для 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));
`последняя прокомментированная строка i, определенная в моем коде для вызова цели проверки. Решите эту проблему.
для TextField только принимающего номера (даже с ограниченной длиной), вы также можете посмотреть на [это вопрос] (http://stackoverflow.com/a/13424140/1076463) – Robin