How do I format JLabel using HTML?

Category: javax.swing, viewed: 9721 time(s).

JLabel displayed text can be formatted using an HTML standard tags. The example below shows you how we can use and HTML font tag to change the font size, color and style of JLabel text.

package org.kodejava.example.swing;

import javax.swing.*;
import java.awt.*;

public class HTMLLabel extends JFrame {
    public HTMLLabel() {
        setTitle("JLabel with HTML");
        initComponents();
    }

    private void initComponents() {
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setSize(400, 200);
        Container container = getContentPane();
        container.setLayout(new FlowLayout(FlowLayout.CENTER));

        //
        // Create a JLabel object that display a string formatted using HTML.
        // 14 font size with red and italic.
        //
        JLabel label = new JLabel("<html><font size=\"14\" color=\"ff0000\"><i>Hello World</i></font></html>");
        container.add(label);
    }

    public static void main(String[] args) {
        SwingUtilities.invokeLater(new Runnable() {
            public void run() {
                new HTMLLabel().setVisible(true);
            }
        });
    }
}
Click here to lend your support to: Kode Java Org and make a donation at www.pledgie.com !

 

Uncensored Newsgroups
Download Hundreds of Complimentary Industry Resources

Get hundreds of popular Industry magazines, white papers, webinars, podcasts, and more; all available at no cost to you. With more than 600 complimentary offers, you'll find plenty of titles to suit your professional interests and needs. Click Here and Sign up today!

Java Training

Sponsored Links

Our Friends

Statistics

Locations of visitors to this page
visitor stats