I had a problem adding text to JTextArea after using DocumentFilter, I need to add a line to JTextArea after the text was loaded from a file, and also return a line from JTextArea of another JFrame to the specified JTextArea
Everything worked fine when I didn't use DocumentFilter.FilterBypass until I added it. It still works a bit, but only when a comma (,) or space ("") is not added. Which does not match the specification I gave.
How can i solve this? Or is there any algorithm or implementation that does not give this problem?
This is insertString code for filtering length and only space and comma are allowed
public void insertString(FilterBypass fb, int offset, String string, AttributeSet attr) throws BadLocationException {
if (isNumeric(string)) {
if (fb.getDocument().getLength() + string.length() > this.length || string.trim().equals("") || string.equals(",")) {
this.insertString(fb, offset, string, attr);
}
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);
}
}
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(.....)