Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Appending a text to a JTextArea after Using a DocumentFilter

Tags:

java

swing

I am a having problem with appending a text to a JTextArea after Using a DocumentFilter, I need to append a string on the JTextArea after the text has been uploaded from a file and also return a string from a JTextArea of another JFrame to the specified JTextArea

Everything worked perfectly when I didn't use the DocumentFilter.FilterBypass until when I added it. It still works a little but only when comma(,) or space(" ") isn't added. Which is not to the specification I was given.

How can I solve this? Or is there any algorithm or implementation that doesn't give this problem?

This is the insertString code to filter the length, and only allow space and comma

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;
}

The other two functions (append from file & append from a different frame) I want to implement innocently by just appending their string values to the JTextArea that is filtered using this. But is being refused by super.insertString(.....)

like image 810
nnanna Avatar asked Nov 14 '22 17:11

nnanna


1 Answers

I'm not sure I really got your question. If you want to have a filter where you can paste complete numbers or "," and whitespace (end or beginning or entered) but cannot paste any other text you can just change your isNumeric function:

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();
}
like image 82
Christian Avatar answered Dec 25 '22 08:12

Christian