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.example.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(400, 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 Runnable() {
            public void run() {
                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.example.swing;

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

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

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

        Icon icon = new ImageIcon("ledgreen.png");
        JLabel label1 = new JLabel("Full Name :", icon, JLabel.LEFT);

        JLabel label2 = new JLabel("Address :", JLabel.LEFT);
        label2.setIcon(new ImageIcon("ledyellow.png"));

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

    public static void main(String[] args) {
        SwingUtilities.invokeLater(new Runnable() {
            public void run() {
                new JLabelWithIcon().setVisible(true);
            }
        });
    }
}

How do I create JLabel component?

package org.kodejava.example.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 Runnable() {
            public void run() {
                new JLabelDemo().setVisible(true);
            }
        });
    }
}

How do I create a multiline tool tips?

package org.kodejava.example.swing;

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

public class MultilineToolTip {
    public static void main(String[] args) {
        JFrame frame = new JFrame("Tool Tip Demo");
        frame.setSize(200, 200);
        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);
    }
}

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

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

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

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

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

JLabel with HTML style