Adding text to JTextArea after using DocumentFilter

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 (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(.....)

+5
2

, . , "," (, ), - , isNumeric:

private boolean isNumeric(String text) {
   text = text.trim();
   if(",".equals(text)) return true;
   ParsePosition position = new ParsePosition(0);
   java.text.NumberFormat.getNumberInstance().parse(text, position);
   return position.getIndex() == text.length();
}
+1

. DocumentFilters, , this.append("stringzzz") ?

, - . , , , - . InsertString() Doc (OffSet)

, - TextPane.getCaretPosition(), . , FilterByPass?

- ( )

this.insertString(TextArea.getCaretPosition(), yourString, null);

, .

FilterByPass

, :)

0

All Articles