File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Swing / AWT / SWT and the fly likes JLabel ..does it accept HTML ? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "JLabel ..does it accept HTML ?" Watch "JLabel ..does it accept HTML ?" New topic

JLabel ..does it accept HTML ?

Jon Camilleri
Ranch Hand

Joined: Apr 25, 2008
Posts: 664

"Beginning with Java SE 1.3, you can use both plain and HTML text in buttons, labels, and
menu items. We don’t recommend HTML in buttons—it interferes with the look and feel. But
HTML in labels can be very effective. Simply surround the label string with <html>. . .</html>,
like this:
label = new JLabel("<html>Required entry:</html>");
Fair warning—the first component with an HTML label takes some time to be displayed
because the rather complex HTML rendering code must be loaded."

Core Java Vol 1 (8th Ed) Pg. 380.

It seems to me that creating a label and passing HTML parameters did not make the item bold (attached scr.JPG - screenshot), however the tags were simply written
out on the label, so no HTML parsing was taking place.

Do I need to write some other code to enable parsing of HTML text?

[Thumbnail for scr.JPG]

Matthew Brown

Joined: Apr 06, 2010
Posts: 4543

Try adding the <html>...</html> tags round the outside of the text, as the instructions you just copied tell you. The JLabel will treat it as plain text if you don't add that.
I agree. Here's the link:
subject: JLabel ..does it accept HTML ?
It's not a secret anymore!