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?
Syntax: System. out. println(ANSI_COLORNAME + "This text is colored" + ANSI_RESET);
Use textArea. append("text") , but I recomend JTextPane for more control like color, selection, etc.
By default, JTextField and JTextArea are editable; you can type and edit in both text components. They can be changed to output-only areas by calling setEditable(false) .
JTextArea
is not a component designed for styled text. If the text can be all one color, call setForeground(Color)
.
Otherwise use a styled text component such as a JEditorPane
or JTextPane
. For more info. on using them, see How to Use Editor Panes and Text Panes.
Also as pointed out by others, the entire String
must start with <html>
.
You cannot use HTML in a JTextArea
, but you can use it with a JEditorPane
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With