How do I associate JLabel component with a JTextField?

In this example we associate a JLabel component with JTextField and JPasswordField using the setLabelFor(). A mnemonic need to be set for the JLabel component and when we press the defined key (ALT + U or ALT + P) a text field will be gained focus.

package org.kodejava.swing;

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

public class JLabelSetForTextField extends JFrame {
    public JLabelSetForTextField() throws HeadlessException {
        initialize();
    }

    private void initialize() {
        setSize(350, 200);
        setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
        setLayout(new FlowLayout(FlowLayout.LEFT));

        JLabel usernameLabel = new JLabel("Username: ");
        JLabel passwordLabel = new JLabel("Password: ");
        JTextField usernameField = new JTextField(20);
        JPasswordField passwordField = new JPasswordField(20);

        // To make the association between the JLabel and JTextField or
        // JPasswordField we need to define the displayed mnemonic and then
        // call JLabel's setLabelFor method.
        usernameLabel.setDisplayedMnemonic(KeyEvent.VK_U);
        usernameLabel.setLabelFor(usernameField);
        passwordLabel.setDisplayedMnemonic(KeyEvent.VK_P);
        passwordLabel.setLabelFor(passwordField);

        getContentPane().add(usernameLabel);
        getContentPane().add(usernameField);
        getContentPane().add(passwordLabel);
        getContentPane().add(passwordField);
    }

    public static void main(String[] args) {
        SwingUtilities.invokeLater(() -> new JLabelSetForTextField().setVisible(true));
    }
}

How do I create JLabel with an image icon?

To create a JLabel with an image icon we can either pass an ImageIcon as a second parameter to the JLabel constructor or use the JLabel.setIcon() method to set the icon.

package org.kodejava.swing;

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

public class JLabelWithIcon extends JFrame {
    public JLabelWithIcon() throws HeadlessException {
        initialize();
    }

    private void initialize() {
        setSize(500, 500);
        setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
        setLayout(new FlowLayout(FlowLayout.LEFT));

        Icon userIcon = new ImageIcon(
                Objects.requireNonNull(this.getClass().getResource("/images/user.png")));
        JLabel userLabel = new JLabel("Full Name :", userIcon, JLabel.LEFT);

        final ImageIcon houseIcon = new ImageIcon(
                Objects.requireNonNull(this.getClass().getResource("/images/house.png")));
        JLabel label2 = new JLabel("Address :", JLabel.LEFT);
        label2.setIcon(houseIcon);

        getContentPane().add(userLabel);
        getContentPane().add(label2);
    }

    public static void main(String[] args) {
        SwingUtilities.invokeLater(() -> new JLabelWithIcon().setVisible(true));
    }
}
JLabel witch Image Icon

JLabel witch Image Icon

How do I create JLabel component?

package org.kodejava.swing;

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

public class JLabelDemo extends JFrame {
    public JLabelDemo() throws HeadlessException {
        initialize();
    }

    private void initialize() {
        setSize(150, 300);
        setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
        setLayout(new FlowLayout());

        // Create some JLabel with Texts and define the horizontal alignment
        JLabel label1 = new JLabel("Username :", JLabel.RIGHT);
        JLabel label2 = new JLabel("Password :", JLabel.RIGHT);
        JLabel label3 = new JLabel("Confirm Password :", JLabel.RIGHT);
        JLabel label4 = new JLabel("Remember Me!", JLabel.LEFT);
        JLabel label5 = new JLabel("Hello, Anybody There?", JLabel.CENTER);

        // Set the vertical alignment for label5 and also set a tool tip for it
        label5.setVerticalAlignment(JLabel.TOP);
        label5.setToolTipText("I have a tool tip with me!");

        getContentPane().add(label1);
        getContentPane().add(label2);
        getContentPane().add(label3);
        getContentPane().add(label4);
        getContentPane().add(label5);
    }

    public static void main(String[] args) {
        SwingUtilities.invokeLater(() -> new JLabelDemo().setVisible(true));
    }
}

How do I create a multiline tool tips?

package org.kodejava.swing;

import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.WindowConstants;
import java.awt.FlowLayout;

public class MultilineToolTip {
    public static void main(String[] args) {
        JFrame frame = new JFrame("Tool Tip Demo");
        frame.setSize(500, 500);
        frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);

        JLabel label = new JLabel("Hover on me!");

        // Setting tool tip for our Swing JLabel component using an html
        // formatted string so that we can create a multi lines tool tip.
        label.setToolTipText(
            "<html>Lorem Ipsum is simply dummy text of the printing and<br/>" +
                "typesetting industry. Lorem Ipsum has been the industry's <br/>" +
                "standard dummy text ever since the 1500s, when an unknown<br/>" +
                "printer took a galley of type and scrambled it to make a<br/>" +
                "type specimen book.</html>");

        frame.getContentPane().setLayout(new FlowLayout(FlowLayout.CENTER));
        frame.getContentPane().add(label);
        frame.setVisible(true);
    }
}
Multiline Tool Tips

Multiline Tool Tips

How do I format JLabel using HTML?

JLabel 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.swing;

import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.SwingUtilities;
import javax.swing.WindowConstants;
import java.awt.Container;
import java.awt.FlowLayout;

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

    public static void main(String[] args) {
        SwingUtilities.invokeLater(() -> new JLabelHTMLStyle().setVisible(true));
    }

    private void initComponents() {
        setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
        setSize(500, 500);
        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.
        String text = "<html>" +
            "<font size='16' color='orange'><strong>Hello World!</strong></font>" +
            "</html>";
        JLabel label = new JLabel(text);
        container.add(label);
    }
}
JLabel with HTML Style

JLabel with HTML Style