How to get StyledDocument.insertString() to update better

708 views Asked by At

I have a background Thread that gets text data from an InputStream and tries to insert it into a JTextPane:

iLen = doc.getLength();
doc.insertString(iLen, lineS, normalStyle);

if ( iLen > 0 )
    textPane.setCaretPosition(doc.getLength() - 1);

Basically my problem is that a lot of the time text is coming through, and it's calling this section of code, but nothing gets drawn till a large section of text has come through.

I understand that a lot is going on behind the scenes and I don't have any Listener or anything.

So is there any fairly simple way I can get it to draw almost every time that it's called?

Or at least more often than it is now?

2

There are 2 answers

1
Thomas On

Try to use textPane.repaint() after setting the position of the caret.

0
StanislavL On

Try to wrap it in SwingUtilities.invokeAndWait() to let EDT execute the insert related changes processing.