JLabel and Icons - potatoscript/JavaSwing GitHub Wiki

🖼️ JLabel and Icons in Java Swing 🎨

🎯 What is JLabel?

JLabel is a GUI component used to display:

  • 📝 Text – Static labels, descriptions, or titles.
  • 🖼️ Icons – Images and icons.
  • 🔄 Combined Text and Icon – Text and icons together.

Key Features:

  • Supports HTML formatting in text.
  • Can be used as an informational display.
  • Aligns text and icons easily.

📚 Step 1: Creating a Basic JLabel


📝 Basic Syntax

import javax.swing.*;

public class MyLabel {
    public static void main(String[] args) {
        // Create JFrame
        JFrame frame = new JFrame("JLabel Example");

        // Create JLabel
        JLabel label = new JLabel("Hello, Java Swing! 🎉");

        // Add label to frame
        frame.add(label);

        // Set frame properties
        frame.setSize(300, 150);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setVisible(true);
    }
}

🎨 Explanation

JLabel label = new JLabel("Hello, Java Swing! 🎉");

  • Creates a JLabel with the text "Hello, Java Swing! 🎉".

frame.add(label);

  • Adds the label to the JFrame.

frame.setSize(300, 150);

  • Sets the window size to 300x150 pixels.

frame.setVisible(true);

  • Displays the frame.

👩‍🎨 Output:

+----------------------------+
| Hello, Java Swing! 🎉       |
+----------------------------+

🎨 Step 2: Adding Icons to JLabel


📝 Basic JLabel with Icon

import javax.swing.*;

public class LabelWithIcon {
    public static void main(String[] args) {
        // Create JFrame
        JFrame frame = new JFrame("Label with Icon");

        // Load Image Icon
        ImageIcon icon = new ImageIcon("icon.png");

        // Create JLabel with Icon
        JLabel label = new JLabel(icon);

        // Add label to frame
        frame.add(label);

        // Set frame properties
        frame.setSize(300, 200);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setVisible(true);
    }
}

🎨 Explanation

ImageIcon icon = new ImageIcon("icon.png");

  • Loads an image from the file icon.png.

JLabel label = new JLabel(icon);

  • Creates a label and attaches the icon.

frame.add(label);

  • Adds the label with the icon to the frame.

👩‍🎨 Output:

+-------------------------+
|  [Image from icon.png]   |
+-------------------------+

🎨 Step 3: Adding Text with Icon


📝 JLabel with Text and Icon

import javax.swing.*;

public class LabelWithTextAndIcon {
    public static void main(String[] args) {
        // Create JFrame
        JFrame frame = new JFrame("Label with Text and Icon");

        // Load Image Icon
        ImageIcon icon = new ImageIcon("icon.png");

        // Create JLabel with text and icon
        JLabel label = new JLabel("Welcome to Java Swing! 🎨", icon, JLabel.CENTER);

        // Add label to frame
        frame.add(label);

        // Set frame properties
        frame.setSize(400, 200);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setVisible(true);
    }
}

🎨 Explanation

JLabel.CENTER

  • Aligns the text and icon to the center.

JLabel label = new JLabel("...", icon, JLabel.CENTER);

  • Combines text and icon in one label.

👩‍🎨 Output:

+-----------------------------------+
| [icon.png]  Welcome to Java Swing!|
+-----------------------------------+

📚 Step 4: Setting Icon Position and Alignment


📝 Position Icon Relative to Text

import javax.swing.*;

public class IconPositionExample {
    public static void main(String[] args) {
        // Create JFrame
        JFrame frame = new JFrame("Icon Position Example");

        // Load Icon
        ImageIcon icon = new ImageIcon("icon.png");

        // Create JLabel with text and icon
        JLabel label = new JLabel("Icon on the Right →", icon, JLabel.LEFT);

        // Set Icon Position
        label.setHorizontalTextPosition(JLabel.LEFT);
        label.setVerticalTextPosition(JLabel.CENTER);

        // Add label to frame
        frame.add(label);

        // Set frame properties
        frame.setSize(400, 200);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setVisible(true);
    }
}

🎨 Explanation

label.setHorizontalTextPosition(JLabel.LEFT);

  • Sets the icon to appear right of the text.

label.setVerticalTextPosition(JLabel.CENTER);

  • Aligns the text and icon vertically.

👩‍🎨 Output:

+-------------------------------+
| Icon on the Right → [icon.png] |
+-------------------------------+

🎨 Step 5: Adding HTML to JLabel


📝 JLabel with HTML Formatting

import javax.swing.*;

public class HtmlLabelExample {
    public static void main(String[] args) {
        // Create JFrame
        JFrame frame = new JFrame("HTML in JLabel");

        // Create JLabel with HTML text
        JLabel label = new JLabel("<html><h1 style='color:blue;'>Hello, <i>Java Swing!</i></h1><br/>" +
                                  "<p style='color:green;'>Welcome to GUI programming.</p></html>");

        // Add label to frame
        frame.add(label);

        // Set frame properties
        frame.setSize(400, 200);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setVisible(true);
    }
}

🎨 Explanation

HTML Support:

  • Swing supports basic HTML tags in JLabel using <html>...</html> syntax.

Formatting Options:

  • <h1>, <p>, <b>, <i>, <br/> – Basic HTML tags.

👩‍🎨 Output:

+-------------------------------+
| Hello, Java Swing!             |
| Welcome to GUI programming.    |
+-------------------------------+

📚 Step 6: Changing Font, Color, and Alignment


📝 Customizing JLabel Appearance

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

public class CustomLabel {
    public static void main(String[] args) {
        // Create JFrame
        JFrame frame = new JFrame("Custom JLabel");

        // Create JLabel
        JLabel label = new JLabel("Styled Label 🎨");

        // Set Font
        label.setFont(new Font("Arial", Font.BOLD, 18));

        // Set Text Color
        label.setForeground(Color.MAGENTA);

        // Set Background and make it opaque
        label.setOpaque(true);
        label.setBackground(Color.LIGHT_GRAY);

        // Align text center
        label.setHorizontalAlignment(JLabel.CENTER);

        // Add label to frame
        frame.add(label);

        // Set frame properties
        frame.setSize(300, 150);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setVisible(true);
    }
}

🎨 Explanation

label.setFont(new Font("Arial", Font.BOLD, 18));

  • Sets the font, style, and size.

label.setForeground(Color.MAGENTA);

  • Sets the text color to magenta.

label.setOpaque(true);

  • Makes the label background visible.

label.setBackground(Color.LIGHT_GRAY);

  • Sets the background color.

👩‍🎨 Output:

+---------------------+
|    Styled Label 🎨   |
+---------------------+

🎨 Step 7: JLabel with Tooltips


📝 Adding Tooltip to JLabel

import javax.swing.*;

public class LabelWithTooltip {
    public static void main(String[] args) {
        // Create JFrame
        JFrame frame = new JFrame("Label with Tooltip");

        // Create JLabel
        JLabel label = new JLabel("Hover over me! 🎈");

        // Set Tooltip
        label.setToolTipText("This is a tooltip. 🎯");

        // Add label to frame
        frame.add(label);

        // Set frame properties
        frame.setSize(300, 150);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setVisible(true);
    }
}

label.setToolTipText("This is a tooltip. 🎯");

  • Displays a tooltip when the mouse hovers over the label.

👩‍🎨 Output:

+--------------------------+
|   Hover over me! 🎈        |
+--------------------------+

➡️ Hovering:

Tooltip: This is a tooltip. 🎯

📚 Step 8: JLabel Methods and Properties


Method Description Example Code
setText(String text) Sets the label text. label.setText("New Text");
getText() Gets the label text. String text = label.getText();
setIcon(Icon icon) Sets an icon on the label. label.setIcon(new ImageIcon("icon.png"));
setHorizontalAlignment(int alignment) Aligns text horizontally. label.setHorizontalAlignment(JLabel.CENTER);
setVerticalAlignment(int alignment) Aligns text vertically. label.setVerticalAlignment(JLabel.TOP);
setHorizontalTextPosition(int position) Sets the icon position horizontally. label.setHorizontalTextPosition(JLabel.RIGHT);
setVerticalTextPosition(int position) Sets the icon position vertically. label.setVerticalTextPosition(JLabel.BOTTOM);
setForeground(Color color) Sets the text color. label.setForeground(Color.RED);
setBackground(Color color) Sets the background color. label.setBackground(Color.YELLOW);
setFont(Font font) Sets the font and size. label.setFont(new Font("Arial", Font.BOLD, 16));
setToolTipText(String text) Adds a tooltip to the label. label.setToolTipText("Info here");

🎁 Bonus: JLabel with Multiple Lines Using HTML

JLabel label = new JLabel("<html>Line 1<br/>Line 2<br/><b>Line 3</b></html>");
⚠️ **GitHub.com Fallback** ⚠️