JLabel Icons - potatoscript/JavaSwing GitHub Wiki

🏷️ JLabel & Icons in Java Swing

1️⃣ Introduction to JLabel

A JLabel is a GUI component in Java Swing used for displaying text, images, or both. It is non-editable and mainly used for UI labeling.

Why Use JLabel?

✔️ Displays text, images, or both
✔️ Supports custom fonts, colors, and alignment
✔️ Works well in layouts like JPanel, JFrame


2️⃣ Creating a Simple JLabel

Example: Adding a Label to a JFrame

import javax.swing.*;

public class JLabelExample {
    public static void main(String[] args) {
        // Create JFrame
        JFrame frame = new JFrame("JLabel Example");
        frame.setSize(300, 200);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

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

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

        // Make frame visible
        frame.setVisible(true);
    }
}

Explanation

✔️ JLabel label = new JLabel("Hello, Swing!"); → Creates a label with text.
✔️ frame.add(label); → Adds the label to the frame.
✔️ By default, text aligns to the left in the container.


3️⃣ Adding an Icon to a JLabel

Example: Displaying an Image in a JLabel

import javax.swing.*;

public class JLabelIconExample {
    public static void main(String[] args) {
        JFrame frame = new JFrame("JLabel with Icon");
        frame.setSize(300, 200);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        // Load image
        ImageIcon icon = new ImageIcon("icon.png"); // Change to your image path

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

        frame.add(label);
        frame.setVisible(true);
    }
}

Explanation

✔️ new ImageIcon("icon.png") → Loads an image file.
✔️ JLabel label = new JLabel(icon); → Creates a label with the image.
✔️ If the image is not found, no error occurs, but the label will be empty.

📌 Make sure the image file exists in the project folder!


4️⃣ Combining Text & Icons in a JLabel

Example: Label with Both Text & Icon

import javax.swing.*;

public class JLabelTextIconExample {
    public static void main(String[] args) {
        JFrame frame = new JFrame("Text & Icon Label");
        frame.setSize(300, 200);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

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

        // Create JLabel with text & icon
        JLabel label = new JLabel("Hello!", icon, JLabel.CENTER);

        frame.add(label);
        frame.setVisible(true);
    }
}

Explanation

✔️ JLabel(label, icon, JLabel.CENTER) → Places text & icon together.
✔️ By default, text is to the right of the icon.


5️⃣ Adjusting Text & Icon Alignment

Example: Changing Text Position

label.setHorizontalTextPosition(JLabel.LEFT);  // Text on the left
label.setHorizontalTextPosition(JLabel.RIGHT); // Text on the right
label.setVerticalTextPosition(JLabel.TOP);     // Text above icon
label.setVerticalTextPosition(JLabel.BOTTOM);  // Text below icon

✔️ setHorizontalTextPosition(JLabel.RIGHT); → Moves text relative to the icon.
✔️ setVerticalTextPosition(JLabel.BOTTOM); → Moves text above or below the icon.


6️⃣ Changing Font, Color, and Style

Example: Customizing JLabel

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

public class StyledLabelExample {
    public static void main(String[] args) {
        JFrame frame = new JFrame("Styled JLabel");
        frame.setSize(300, 200);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        JLabel label = new JLabel("Styled Text");
        label.setFont(new Font("Arial", Font.BOLD, 18)); // Set font
        label.setForeground(Color.BLUE); // Set text color

        frame.add(label);
        frame.setVisible(true);
    }
}

Explanation

✔️ setFont(new Font("Arial", Font.BOLD, 18)); → Changes font size & style.
✔️ setForeground(Color.BLUE); → Changes text color.


7️⃣ Using HTML for Multi-line Text

Example: Multi-line Label with HTML

JLabel label = new JLabel("<html>Hello<br>Welcome to Swing!</html>");

✔️ Use <html>...</html> tags to add multi-line text inside a JLabel.


8️⃣ Centering JLabel in a Panel

Example: Center Aligning a Label

label.setHorizontalAlignment(JLabel.CENTER);

✔️ Centers the label within its container.


🔥 Summary

JLabel is used to display text & images.
✅ Supports icons, colors, fonts, and alignment.
✅ Use HTML to create multi-line labels.
Combine text & icons with JLabel.

⚠️ **GitHub.com Fallback** ⚠️