Keep JScrollPane as same length java

I'm not sure if this is what you're looking for, but in the past I've created a JTextPane that was constantly updated with exception messages, each colour coded. It had a scroll pane, and a text size buffer so when reached, it will remove from the start the amount of text appended to the end.

Every time I caught an exception, I ran this:

printExceptionMessage(new TCException(ex));

Which in turn:

protected void printExceptionMessage(final TCException exception) {

    SwingUtilities.invokeLater(
        new Runnable() {
            public void run() {

                Date recordDate = exception.getTimeStamp();
                SimpleDateFormat sdf = new SimpleDateFormat("yyyy.MM.dd 'at' HH:mm:ss z");
                String recordTime = sdf.format(recordDate);

                String message = "(" + recordTime + ") EXCEPTION:   " + exception.getMessage() + "\n";

                statusColorTextPane.setEditable(true);

                // trim the textPane to the buffer size
                Document doc = statusColorTextPane.getStyledDocument();
                int overLength = doc.getLength() + message.length() - Foo.TEXT_BUFFER_SIZE;

                if (overLength > 0) {
                    try {
                        doc.remove(0, overLength);
                    } catch (Exception ex) {

                        System.err.println("Error occured while trimming the textPane.");
                        ex.printStackTrace();
                    }
                }
                statusColorTextPane.append(Color.red, message);
                statusColorTextPane.setEditable(false);
            }
        });     
}

In this case, statusColorTextPane is an extended class of JTextPane. It needs these two methods in it:

public void appendNaive(Color c, String s) { // naive implementation

    SimpleAttributeSet aset = new SimpleAttributeSet();
    StyleConstants.setForeground(aset, c);

    int len = getText().length();
    setCaretPosition(len); // place caret at the end (with no selection)
    setCharacterAttributes(aset, false);
    replaceSelection(s); // there is no selection, so inserts at caret
}

and

public void append(Color c, String s) { // better implementation--uses StyleContext

    StyleContext sc = StyleContext.getDefaultStyleContext();
    AttributeSet aset = sc.addAttribute(SimpleAttributeSet.EMPTY,
                                    StyleConstants.Foreground, c);

    int len = getDocument().getLength(); // same value as getText().length();
    setCaretPosition(len);  // place caret at the end (with no selection)
    setCharacterAttributes(aset, false);
    replaceSelection(s); // there is no selection, so inserts at caret
}

Foo.TEXT_BUFFER_SIZE for me I have set to 20000.