Introduction
The (useful) answers for how to do what you want to do have already been posted by @Freek de Bruijn and @Gilbert Le Blanc, but none of them explain why what you're trying to do doesn't work. This isn't an answer for
How can I do that?
but an explanation for
But it does not work.
Edit: @camickr posted what I believe is the correct approach.
Answer
From the tutorial about about JTextArea
:
You can customize text areas in several ways. For example, although a given text area can display text in only one font and color, you can set which font and color it uses.
(all emphasis in quotes are mine) and
If you want the text area to display its text using multiple fonts or other styles, you should use an editor pane or text pane.
This is because JTextArea
uses PlainDocument
(see this):
PlainDocument
provides a basic container for text where all the text is displayed in the same font.
However, a JTextPane
uses DefaultStyledDocument
:
a container for styled text in no particular format.