Klausos Klausos
Klausos Klausos

Reputation: 16040

How to change the color of particular characters in JLabel?

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

Answers (3)

alain.janinm
alain.janinm

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

Tim B&#252;the
Tim B&#252;the

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

Marcelo
Marcelo

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

Related Questions