Reputation: 16040
How to change the color of particular characters in JLabel? For instance,
JLabel lbl = new JLabel("My Text");
I would like that My
has blue color, while Text
has red color.
Is it possible?
Upvotes: 5
Views: 2969
Reputation: 20065
Try with html:
String labelText =
"<html><FONT COLOR=BLUE>My</FONT>" +
"<FONT COLOR=RED>Text</FONT></html>";
JLabel lbl = new JLabel(labelText);
Upvotes: 4
Reputation: 63734
You can use HTML in JLabels, so this should work:
JLabel lbl = new JLabel("<html><span style='color: blue;'>My</span> <span style='color: red;'>Text</span></html>");
Upvotes: 3
Reputation: 4608
Yes, check How to Use HTML in Swing Components.
JLabel lbl = new JLabel("<html><font color=blue>My</font><font color=red>Text</font></html>");
Upvotes: 6