BJ Dela Cruz
BJ Dela Cruz

Reputation: 5354

Changing color of some text in a JLabel

I have a JLabel with text in it, and I want to append another piece of text to it, but the latter will be of a different color than the former (e.g. red). I've tried:

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

But it doesn't work. It just shows the HTML tags but does not render them. Any suggestions? Is it possible to change the color of some of the text in a JLabel?

Upvotes: 7

Views: 10168

Answers (1)

tenorsax
tenorsax

Reputation: 21223

Try this:

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

Or for you case you can build the string like this:

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

Upvotes: 11

Related Questions