Change the color of any text in JLabel

I have a JLabel with text in it, and I want to add another piece of text to it, but the latter will have a different color than the first (for example, red). I tried:

statusLabel.setText(statusLabel.getText() + " <html><span style\"color: red\">" + message + "</span></html>"); 

But that will not work. It simply displays HTML tags but does not display them. Any suggestions? Is it possible to change the color of some text in JLabel?

+6
source share
1 answer

Try the following:

 setText("<html>Some text <font color='red'>some text in red</font></html>"); 

Or for the case, you can build the string as follows:

 statusLabel.setText(String.format("<html>%s<font color='red'>%s</font></html>", statusLabel.getText(), message)); 
+9
source

Source: https://habr.com/ru/post/914180/


All Articles