1

I am attempting to set multiple text colors for a String in a JTextArea. I have tried multiple things such as:

<html>Text color: <font color='red'>red</font><font color ="black">Hi</font></html>
<html>Text color: <font color="red">red</font><font color ="black">Hi</font></html>

But it just appears as that itself. I read things such as use JEditorPane or JTextPane and I thought of it but I'm unfamiliar with all the things that'd require to use. I'd have to learn it eventually but not now.

EDIT: I have learned that you can't use colors on JTextArea

MadProgrammer
  • 343,457
  • 22
  • 230
  • 366
  • 2
    JTextArea wont support html..JEditorPane is the only option and it's almost same as JTextArea .why bother using that – Madhan Jul 04 '15 at 06:27
  • Thanks, I didn't know that. I don't know if you mean the HTML of JTextArea but alas: JTextArea I was introduced to first and it was mainly my go-to so I'm very familiar with it. As for the HTML, well, I'm familiar with it (vaguely) but mainly due to being familiar with JTextArea so much. @Madhan – Alex Trevoso Jul 04 '15 at 06:42
  • JTextArea only supports plain text, you can use JTextPane wch supports a styled document or a JEditorPane which can support a HTML document – MadProgrammer Jul 04 '15 at 06:48
  • Have a look at [How to Use Editor Panes and Text Panes](http://docs.oracle.com/javase/tutorial/uiswing/components/editorpane.html) for more options – MadProgrammer Jul 04 '15 at 06:53
  • Have a look at http://stackoverflow.com/questions/9650992/how-to-change-text-color-in-the-jtextarea – Anptk Jul 04 '15 at 07:31

1 Answers1

2

No. JTextArea cannot be used for that. It supports only and only plain text. Whatever HTML you write in it, will appear as it is.

As an alternative, you can either use JTextPane or JEditorPane.

Check docs:

JTextPane: http://docs.oracle.com/javase/7/docs/api/javax/swing/JTextPane.html

JEditorPane: http://docs.oracle.com/javase/7/docs/api/javax/swing/JEditorPane.html

Aditya Singh
  • 2,343
  • 1
  • 23
  • 42