5
votes

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

2
What code are you using at the moment that is not working? Mind posting it? - sealz
No problem, But its long though. Have to attach the codes of the two frames - nnanna
maybe just the snippet where you are trying to add the "," and " "? - sealz
But the specification I was given mandates me to add them. I cant do away with them - nnanna
How can I get around it. The javadoc specific at all on doing that - nnanna

2 Answers

1
votes

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();
}
0
votes

You may need to get the carat location before appending the text. I am not to familiar with DocumentFilters and i am assuming that the this.append("stringzzz") method is not available?

It seems there is something wrong with yout offset. You may need to set it up to get a position first, something like below. InsertString() Doc (OffSet)

As for getting the carat position you could do something like TextPane.getCaretPosition(), and pass that in. Instead of using the FilterByPass?

Something like(as suggested in my link)

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

Here is a link that may be of some help.

Inserting Text without FilterByPass

Let me know if I am way off :)