Java - setting Swing color for text in JTextArea

I have a JTextArea that has its text set to an information line. In this line of information, I have a variable that I would like to color red, for this I will edit the line as follows:

"Result: <html><font color=red>" + negativeValue + "</font></html>"

I expect this to yield Result: ##, where the number is red. However, it simply puts the following in the text area:

Result: <html><font color=red>##</font></html>

I'm not sure how to do this, so can someone advise how to do this?

+5
source share
5 answers

JTextAreaNot a component designed for stylized text. If the text can be the same color, call setForeground(Color).

, JEditorPane JTextPane. . , , . .

, , String <html>.

+8

, JTextArea . HTML JEditorPane .

+1

HTML JTextArea, JEditorPane

+1

JTextArea , HTML JEditorPane JTextPane. . .

+1

Java only displays html code if it starts with <html>. You should try this as

"<html>Result: <font color=red>" + negativeValue + "</font></html>"
0
source

All Articles