Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Changing color of some text in a JLabel

Tags:

java

swing

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?

like image 584
BJ Dela Cruz Avatar asked Apr 26 '12 00:04

BJ Dela Cruz


1 Answers

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));
like image 85
tenorsax Avatar answered Oct 14 '22 01:10

tenorsax