How to create JLabel to hold multiline of text using HTML in Java?

In Java Swing, the JLabel component by default displays only single-line text. To create a JLabel that can hold multiple lines of text, we need to use HTML formatting within the label text. This allows us to use HTML tags like <br> for line breaks and other HTML formatting elements.

Syntax

Following is the syntax to create a multiline JLabel using HTML −

JLabel label = new JLabel("<html>Line1<br>Line2</html>");

For more complex formatting with alignment −

JLabel label = new JLabel("<html><strong>Line1<br>Line2</strong></html>", JLabel.LEFT);

How It Works

When a JLabel's text starts with <html>, Swing's HTML renderer processes the content and applies HTML formatting. The <br> tag creates line breaks, while other HTML tags like <strong>, <em>, and <font> can be used for text styling.

Key HTML tags useful in JLabel −

  • <br> − Creates line breaks
  • <strong> − Makes text bold
  • <em> − Makes text italic
  • <font> − Changes font color and size
  • <center> − Centers text alignment

Example − Basic Multiline JLabel

Following example demonstrates creating a simple multiline JLabel using HTML −

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

public class MultilineJLabelDemo {
    public static void main(String args[]) {
        JFrame frame = new JFrame("Multiline JLabel Example");
        
        // Create multiline JLabel with HTML
        JLabel label = new JLabel("<html><strong>Welcome to TutorialsPoint<br>Java Swing Tutorial</strong></html>", JLabel.LEFT);
        
        label.setBounds(50, 50, 300, 60);
        label.setFont(new Font("Arial", Font.PLAIN, 14));
        
        frame.add(label);
        frame.setSize(400, 200);
        frame.setLayout(null);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setVisible(true);
    }
}

The output displays a JLabel with two lines of bold text −

Welcome to TutorialsPoint
Java Swing Tutorial

Example − Advanced HTML Formatting

Following example shows more advanced HTML formatting options in JLabel −

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

public class AdvancedMultilineJLabel {
    public static void main(String args[]) {
        JFrame frame = new JFrame("Advanced Multiline JLabel");
        
        // Create JLabel with complex HTML formatting
        String htmlText = "<html>" +
                         "<center><font color='blue' size='5'><strong>TutorialsPoint</strong></font></center>" +
                         "<br>" +
                         "<font color='green'>Learn Java Programming</font><br>" +
                         "<font color='red'><em>Swing GUI Components</em></font><br>" +
                         "<font color='purple'>HTML in JLabel</font>" +
                         "</html>";
        
        JLabel label = new JLabel(htmlText);
        label.setBounds(50, 30, 300, 120);
        
        frame.add(label);
        frame.setSize(400, 200);
        frame.setLayout(null);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setVisible(true);
    }
}

This creates a formatted multiline label with different colors and styles −

        TutorialsPoint        (blue, bold, large, centered)
Learn Java Programming        (green text)
Swing GUI Components          (red italic text)
HTML in JLabel               (purple text)

Example − Using HTML Lists in JLabel

Following example demonstrates using HTML list formatting in JLabel −

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

public class JLabelWithHTMLList {
    public static void main(String args[]) {
        JFrame frame = new JFrame("JLabel with HTML List");
        
        String listHTML = "<html>" +
                         "<strong>Java Topics:</strong><br>" +
                         "<ul>" +
                         "<li>Object-Oriented Programming</li>" +
                         "<li>Exception Handling</li>" +
                         "<li>Collections Framework</li>" +
                         "<li>Swing GUI</li>" +
                         "</ul>" +
                         "</html>";
        
        JLabel listLabel = new JLabel(listHTML);
        listLabel.setBounds(30, 30, 300, 150);
        listLabel.setVerticalAlignment(SwingConstants.TOP);
        
        frame.add(listLabel);
        frame.setSize(400, 250);
        frame.setLayout(null);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setVisible(true);
    }
}

The output shows a properly formatted bulleted list within the JLabel −

Java Topics:
? Object-Oriented Programming
? Exception Handling
? Collections Framework
? Swing GUI
JLabel Text Handling Plain Text JLabel Single line only No formatting Simple display "Hello World" HTML JLabel Multiple lines Rich formatting Colors, fonts, lists "<html>Line1<br>Line2</html>"

Key Points

  • HTML Start Tag − The text must begin with <html> to enable HTML rendering in JLabel.
  • Line Breaks − Use <br> tags to create line breaks instead of \n.
  • Text Formatting − HTML tags like <strong>, <em>, <font> work within JLabel.
  • Performance − HTML rendering is slower than plain text, so use it only when needed.
  • Size Adjustment − Increase the JLabel's height to accommodate multiple lines of text.

Conclusion

Creating multiline JLabels in Java Swing requires using HTML formatting with the <html> tag and <br> elements for line breaks. This approach enables rich text formatting including colors, fonts, lists, and other HTML styling within JLabel components, making them more versatile for complex GUI layouts.

Updated on: 2026-03-16T21:38:53+05:30

1K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements