Take the 2-minute tour ×

I am creating a chat application. In this chat application, I have to use Java Swing's JTextPane to give style to some specific strings like the user's display name and the message's time. That's why I chose JTextPane over JTextArea. In JTextPane, I can't append the String, so I create a String buffer to append the String, then I do jTextPane.setText(stringBuffer.toString()).

Here is my problem: When I append a string into the JTextPane via a StringBuffer, there is a delay. How can I append a string line-by-line in a JTextPane?

share|improve this question
Get the Document object of the JTextPane and use the insertString() method –  Hunter McMillen Jun 28 '12 at 12:53
Please have a look at this example –  nIcE cOw Jun 28 '12 at 15:34
add comment

1 Answer

up vote 4 down vote accepted

Found another similar question whose answer made use of the StyledDocument and calls insertString() method on the instance of StyledDocument to append text to it and then write the StyledDocument back to JTextPane: JTextPane appending a new string

share|improve this answer
For example. –  trashgod Jun 28 '12 at 12:54
Thank you!i will try this.. –  boopathy Jun 28 '12 at 14:32
Thank you! Its work. –  boopathy Jun 28 '12 at 17:46
@boopathy Make sure it's set as the answer if it works. –  Igor Oct 22 '12 at 14:24
add comment

Your Answer


