1

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?

mKorbel
  • 109,525
  • 20
  • 134
  • 319
boopathy
  • 427
  • 2
  • 9
  • 20
  • 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](http://stackoverflow.com/questions/9650992/how-to-change-text-color-in-the-jtextarea/9652143#9652143) – nIcE cOw Jun 28 '12 at 15:34
  • possible duplicate of [JTextPane appending a new string](http://stackoverflow.com/questions/4059198/jtextpane-appending-a-new-string) – PhoneixS Oct 15 '14 at 08:34

1 Answers1

6

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

Community
  • 1
  • 1
David Kroukamp
  • 36,155
  • 13
  • 81
  • 138