Fading Coder

One Final Commit for the Last Sprint

Home > Tech > Content

Working with JLabel Components and Custom Icons in Swing

Tech May 8 4

Swing provides the JLabel component for displaying read-only text, images, or combinations of both. Unlike interactive components, labels do not respond to user events—they serve purely decorative and informational purposes. However, JLabel offers alignment properties to control how content positions itself within the component.

JLabel Class Overview

The JLabel class offers multiple constructors to accommodate different initialization needs:

public JLabel() // Empty label with no text or icon
public JLabel(Icon icon) // Label displaying only an icon
public JLabel(Icon icon, int horizontalAlignment) // Icon with specified horizontal alignment
public JLabel(String text, int horizontalAlignment) // Text with specified horizontal alignment
public JLabel(String text, Icon icon, int horizontalAlignment) // Text and icon with alignment

The alignment parameter accepts constants from SwingConstants such as LEFT, CENTER, and RIGHT.

Implementing the Icon Interface

Swing uses the Icon interface for custom graphics. To create a personalized icon, implement three required methods:

public int getIconHeight() // Returns the icon's vertical dimension
public int getIconWidth() // Returns the icon's horizontal dimension
public void paintIcon(Component c, Graphics g, int x, int y) // Renders the icon at coordinates (x, y)

Complete Implementtaion Example

The following example demonstrates creating a circular icon and displaying it within a label:

import java.awt.Component;
import java.awt.Container;
import java.awt.Graphics;
import javax.swing.Icon;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.SwingConstants;
import javax.swing.WindowConstants;

public class CircleIcon implements Icon {
    private int diameterWidth;
    private int diameterHeight;

    public CircleIcon(int w, int h) {
        this.diameterWidth = w;
        this.diameterHeight = h;
    }

    @Override
    public int getIconHeight() {
        return this.diameterHeight;
    }

    @Override
    public int getIconWidth() {
        return this.diameterWidth;
    }

    @Override
    public void paintIcon(Component canvas, Graphics context, int posX, int posY) {
        context.fillOval(posX, posY, diameterWidth, diameterHeight);
    }

    public static void main(String[] args) {
        CircleIcon circle = new CircleIcon(15, 15);
        JLabel label = new JLabel("Demo", circle, SwingConstants.CENTER);

        JFrame frame = new JFrame();
        Container panel = frame.getContentPane();
        panel.add(label);

        frame.setSize(120, 80);
        frame.setVisible(true);
        frame.setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
    }
}

This implementation renders a filled oval using the Graphics API within the paintIcon method. The label centers its content both horizontally and vertical by default, providing a clean visual presentation.

Practical Workflow

Icons are typically attached to buttons or labels. The pattern involves instantiating a custom Icon implementation, passing it to a JLabel constructor, and adding the label to a container. This approach enables rich visual elements throughout your Swing application interface.

Related Articles

Understanding Strong and Weak References in Java

Strong References Strong reference are the most prevalent type of object referencing in Java. When an object has a strong reference pointing to it, the garbage collector will not reclaim its memory. F...

Comprehensive Guide to SSTI Explained with Payload Bypass Techniques

Introduction Server-Side Template Injection (SSTI) is a vulnerability in web applications where user input is improper handled within the template engine and executed on the server. This exploit can r...

Implement Image Upload Functionality for Django Integrated TinyMCE Editor

Django’s Admin panel is highly user-friendly, and pairing it with TinyMCE, an effective rich text editor, simplifies content management significantly. Combining the two is particular useful for bloggi...

Leave a Comment

Anonymous

◎Feel free to join the discussion and share your thoughts.