JLabel with different color

S

shoa

Hello

I have a JLabel in my application. What I want is to create a different
color for the text in that label. For example, "My" is red color and "label"
is green color for a label text "My label". Could you please tell me how to
do that.

Thank you
S.Hoa
 
B

Betty

shoa said:
Hello

I have a JLabel in my application. What I want is to create a different
color for the text in that label. For example, "My" is red color and "label"
is green color for a label text "My label". Could you please tell me how to
do that.

Thank you
S.Hoa
There is some way to specify the text using html syntax, look
that up. Then "<font color=red>My </font><font color=green>label</font>"
should do it.
 
L

Lance

shoa said:
Hello

I have a JLabel in my application. What I want is to create a
different color for the text in that label. For example, "My" is red
color and "label" is green color for a label text "My label". Could
you please tell me how to do that.

Thank you
S.Hoa

JLabel has embedded capability to render html text. So the following code
will do what you expect.

JLabel label = new JLabel("<html><font color="FF0000">My</font> <font
color="00FF00">label</font></html>");

Cheers,
Lance
 
C

Chris Smith

shoa said:
I have a JLabel in my application. What I want is to create a different
color for the text in that label. For example, "My" is red color and "label"
is green color for a label text "My label". Could you please tell me how to
do that.

You can put all kinds of formatting in a JLabel by wrapping the text in
<html>...</html> and expressing the formatting in HTML.

--
www.designacourse.com
The Easiest Way To Train Anyone... Anywhere.

Chris Smith - Lead Software Developer/Technical Trainer
MindIQ Corporation
 
S

shoa

Thank you
However, I cannot setText for this JLabel

For example:

JLabel aLabel = new JLabel();
....
aLabel.setText ("Question " + i + "(*)")
I want "(*)" with red color

Thanks
S.Hoa
 
B

Bjorn Abelli

...
However, I cannot setText for this JLabel

For example:

JLabel aLabel = new JLabel();
...
aLabel.setText ("Question " + i + "(*)")
I want "(*)" with red color

aLabel.setText("<html>Question " + i + " <font
color=#FF0000>(*)</font></html>");

// Bjorn A
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,432
Messages
2,571,680
Members
48,796
Latest member
Greg L.

Latest Threads

Top