У меня проблема с добавлением текста в JTextArea после использования DocumentFilter, мне нужно добавить строку в JTextArea после того, как текст был загружен из файла, а также вернуть строку из JTextArea другого JFrame вуказанный JTextArea
Все работало отлично, когда я не использовал DocumentFilter.FilterBypass, пока я не добавил его.Это все еще работает немного, но только когда запятая (,) или пробел ("") не добавлены.Что не соответствует спецификации, которую мне дали.
Как я могу решить эту проблему?Или есть какой-либо алгоритм или реализация, которая не создает этой проблемы?
Это код insertString для фильтрации длины и разрешения только пробела и запятой
public void insertString(FilterBypass fb, int offset, String string, AttributeSet attr) throws BadLocationException {
// if (string == null || string.trim().equals("") || string.equals(","))
// {
// return;
// }
if (isNumeric(string)) {
// if (this.length > 0 && fb.getDocument().getLength() +
// string.length()
// > this.length) {
// return;
// }
if (fb.getDocument().getLength() + string.length() > this.length || string.trim().equals("") || string.equals(",")) {
this.insertString(fb, offset, string, attr);
}
// if (string == null || string.trim().equals("") ||
// string.equals(",")) {
// return;
// }
super.insertString(fb, offset, string, attr);
}
else if (string == null || string.trim().equals("") || string.equals(",")) {
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("") || text.equals(",")) {
return true;
}
for (int iCount = 0; iCount < text.length(); iCount++) {
if (!Character.isDigit(text.charAt(iCount))) {
return false;
}
}
return true;
}
Другие две функции(добавить из файла и добавить из другого фрейма) Я хочу реализовать невинно, просто добавив их строковые значения в JTextArea, который фильтруется с помощью этого.Но super.insertString (.....)
получает отказ