JLabel

1. JLabel Basics

In many cases, JLabel is used exactly like Label: as a way to display text. However, since a JLabel can have an image instead of or in addition to the text, it is also frequently used as a way to put an image in a display.

2. New Features: Images, Borders, and HTML Content

A JLabel has three major features that Label does not.

The first is the ability to display images, usually by supplying an ImageIcon eiher to the constructor or via a call to setIcon. The use of icons in JLabel is just like the use in JButton, so see the Swing Tutorial JButton section for more details and code examples. For a quick preview, however, see the third JLabel in the example below.

The second new feature is the ability to place borders around the labels. The use of borders is covered in the Swing tutorial section on JPanel. For a quick preview, however, see the following examples which use titled borders.

JLabels with HTML text
JLabels with HTML Text

The third new feature, and the one I am focusing on here, is the ability to use HTML to format the label. The idea is that, if the string for the label begins with "<html>", then the string is interpreted as HTML rather than taken literally. This lets you make multi-line labels, labels with mixed colors and fonts, and various other fancy effects. This capability also applies to JButton. Although nice, this capabillity also has several significant limitations:

3. JLabels with HTML: Example (Download source code)

The following example produces the result shown in the image above:
import java.awt.*;
import javax.swing.*;

public class JLabels extends JFrame {
  public static void main(String[] args) {
    new JLabels();
  }

  public JLabels() {
    super("Using HTML in JLabels");
    WindowUtilities.setNativeLookAndFeel();
    addWindowListener(new ExitListener());
    Container content = getContentPane();
    Font font = new Font("Serif", Font.PLAIN, 30);
    content.setFont(font);
    String labelText =
      "<html><FONT COLOR=RED>Red</FONT> and " +
      "<FONT COLOR=BLUE>Blue</FONT> Text</html>";
    JLabel coloredLabel =
      new JLabel(labelText, JLabel.CENTER);
    coloredLabel.setBorder
      (BorderFactory.createTitledBorder("Mixed Colors"));
    content.add(coloredLabel, BorderLayout.NORTH);
    labelText =
      "<html><B>Bold</B> and <I>Italic</I> Text</html>";
    JLabel boldLabel =
      new JLabel(labelText, JLabel.CENTER);
    boldLabel.setBorder
      (BorderFactory.createTitledBorder("Mixed Fonts"));
    content.add(boldLabel, BorderLayout.CENTER);
    labelText =
      "<html>The Applied Physics Laboratory is a division " +
      "of the Johns Hopkins University." +
      "<P>" +
      "Major JHU divisions include:" +
      "<UL>" +
      "  <LI>The Applied Physics Laboratory" +
      "  <LI>The Krieger School of Arts and Sciences" +
      "  <LI>The Whiting School of Engineering" +
      "  <LI>The School of Medicine" +
      "  <LI>The School of Public Health" +
      "  <LI>The School of Nursing" +
      "  <LI>The Peabody Institute" +
      "  <LI>The Nitze School of Advanced International Studies" +
      "</UL>";
    JLabel fancyLabel =
      new JLabel(labelText,
                 new ImageIcon("images/JHUAPL.gif"),
                 JLabel.CENTER);
    fancyLabel.setBorder
      (BorderFactory.createTitledBorder("Multi-line HTML"));
    content.add(fancyLabel, BorderLayout.SOUTH);
    pack();
    setVisible(true);
  }
}
Note: also requires WindowUtilities.java and ExitListener.java, shown earlier, plus JHUAPL.gif.


This page is part of my Quick Swing Tutorial for AWT Programmers. © 1999 Marty Hall. All source code freely available for unrestricted use. Created for for work in the Research and Technology Development Center of the Johns Hopkins University Applied Physics Lab, for courses in the Johns Hopkins Part-Time MS Program in Computer Science, and for various industry seminars and Java short courses.