How to use html tags in JTextArea

user1508381 picture user1508381 · Nov 2, 2012 · Viewed 26.9k times · Source

When I try to change color of a JTextArea using

textArea.setText("<html> <font color=\"red\"> Hi </font></html>")

, the text is shown is basically the whole text written inside setText method. But similar things work for JLabel, JButton etc.

How can I do that for a JTextArea also?

Answer

mKorbel picture mKorbel · Nov 2, 2012
  • don't to use JTextArea for Html formatted Object (even is possible, but why bothering)

  • use JTextPane / JEditorPane instead (setContentType("text/html"))

  • notice, todays Java and methods implemented in Official API by Oracle supporting only reduced Html syntax with css <= Html 3.2

  • use JavaFX in the case that you want/need to use todays Html5,