I have a JTextArea which has its text set to a string of information. In this string of information I have a variable which I would like coloured red, to do this I edit the string as follows:
"Result: <html><font color=red>" + negativeValue + "</font></html>"
I would expect this to give Result: ## where the number is red. However it just puts the following into the text area:
Result: <html><font color=red>##</font></html>
I’m not really sure how to get this working, so could someone offer advice as to how to do so?
JTextAreais not a component designed for styled text. If the text can be all one color, callsetForeground(Color).Otherwise use a styled text component such as a
JEditorPaneorJTextPane. For more info. on using them, see How to Use Editor Panes and Text Panes.Also as pointed out by others, the entire
Stringmust start with<html>.