Skip to Main Content

Java SE (Java Platform, Standard Edition)

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

[JLabel] HTML + Font

843806Nov 12 2008 — edited Nov 15 2008
Hi,

I have a JLabel in which I use HTML to color my text (the color is not the same for all the text)?

ex :

JLabel label = new JLabel("<html><body>font color='blue'>hi</font> to <font color='red'>you</font></body></html>);

Then, I set a customized font that I load dynamically:

InputStream inputFont = this.getClass().getResourceAsStream("my_font.ttf");
Font font = Font.createFont(Font.TRUETYPE_FONT, inputFont);
MY_FONT = font.deriveFont(24f);

label.setFont(MY_FONT);

*My problem is:*

When I do that, the font is not applied (problably because of HTML font tags). I just see for a moment my font with the HTML tags, and when the HTML is parsed, the default font comes back. I've tried to change <font> by <span>, it does just the same)

When I remove HTML tags from my JLabel, like new JLabel("plop") and apply my customized font, I have no problem and my font is applied (but I don't have the colors that I want).


Problem is I want both of it! My customized font AND colors.

Do you know how to do that?

I hope I made myself clear,
Thanks :)
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on Dec 13 2008
Added on Nov 12 2008
20 comments
599 views