How do I customize JButton icons?

The example below shows you how we can customize the icon for JButton Swing components.

package org.kodejava.swing;

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

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

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

        JButton button = new JButton("Press Me!");

        // Below is how we can set various icons for the JButton swing
        // component. There are the default icon, selected, disabled, pressed
        // and rollover icons.
        button.setIcon(new ImageIcon("default.png"));
        button.setSelectedIcon(new ImageIcon("selected.png"));
        button.setDisabledIcon(new ImageIcon("disabled.png"));
        button.setDisabledSelectedIcon(new ImageIcon("disabledSelected.png"));
        button.setPressedIcon(new ImageIcon("pressed.png"));
        button.setRolloverIcon(new ImageIcon("rollover.png"));
        button.setRolloverSelectedIcon(new ImageIcon("rolloverSelected.png"));

        getContentPane().add(button);
    }

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

How do I customize JCheckBox icons?

JCheckBox can have a different icon for its states, there are default icon, a selected icon, disabled icon, pressed icon or rollover icon, etc. See the code below and have a try on it.

package org.kodejava.swing;

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

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

    private void initialize() {
        setSize(500, 500);
        setTitle("JCheckBox Icon Demo");
        setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
        setLayout(new FlowLayout(FlowLayout.LEFT));

        // Creating checkbox with text label
        JCheckBox checkBox = new JCheckBox("Check me!");
        checkBox.setSelected(true);

        // Set default icon for checkbox
        checkBox.setIcon(new ImageIcon("icon.png"));
        // Set selected icon when checkbox state is selected
        checkBox.setSelectedIcon(new ImageIcon("selectedIcon.png"));
        // Set disabled icon for checkbox
        checkBox.setDisabledIcon(new ImageIcon("disabledIcon.png"));
        // Set disabled-selected icon for checkbox
        checkBox.setDisabledSelectedIcon(new ImageIcon("disabledSelectedIcon.png"));
        // Set checkbox icon when checkbox is pressed
        checkBox.setPressedIcon(new ImageIcon("pressedIcon.png"));
        // Set icon when a mouse is over the checkbox
        checkBox.setRolloverIcon(new ImageIcon("rolloverIcon.png"));
        // Set icon when a mouse is over a selected checkbox
        checkBox.setRolloverSelectedIcon(new ImageIcon("rolloverSelectedIcon.png"));

        getContentPane().add(checkBox);
    }

    public static void main(String[] args) {
        SwingUtilities.invokeLater(() -> new JCheckBoxCustomIcon().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 change JFrame image icon?

This code snippet demonstrates how to change a JFrame image icon using the setIconImage() method.

package org.kodejava.swing;

import javax.imageio.ImageIO;
import javax.swing.JFrame;
import javax.swing.WindowConstants;
import java.awt.Dimension;
import java.awt.image.BufferedImage;
import java.io.IOException;
import java.net.URL;

public class FrameIconExample extends JFrame {
    public static void main(String[] args) {
        FrameIconExample frame = new FrameIconExample();
        frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);

        // Set the window size and its title
        frame.setSize(new Dimension(500, 500));
        frame.setTitle("Frame Icon Example");

        // Read the image that will be used as the application icon.
        // Using "/" in front of the image file name will locate the
        // image at the root folder of our application. If you don't
        // use a "/" then the image file should be on the same folder
        // with your class file.
        try {
            URL resource = frame.getClass().getResource("/logo.png");
            BufferedImage image = ImageIO.read(resource);
            frame.setIconImage(image);
        } catch (IOException e) {
            e.printStackTrace();
        }

        // Display the form
        frame.setVisible(true);
    }
}
Change JFrame Image Icon

Change JFrame Image Icon