How do I add disabled icon for JTabbedPane tabs?

When adding a tabs to JTabbedPane you can define the icon for the tabs. This icon will be displayed beside the tab’s title in enable or disable state of the tabs. To make the user interface better you can also define a disabled icon for the tab. This icon will be displayed when the state of the tabs are disabled.

To assign a disabled icon for the tab use the JTabbedPane‘s setDisabledIconAt(int index, Icon icon) method. As always the index parameter is zero-based which means the first tab is at index number 0. The icon parameter is the disabled icon of your tabs.

package org.kodejava.swing;

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

public class TabbedPaneDisableIcon extends JPanel {
    private final ImageIcon[] disableIcons = {
            new ImageIcon(Objects.requireNonNull(
                    this.getClass().getResource("/images/test-pass-icon-disable.png"))),
            new ImageIcon(Objects.requireNonNull(
                    this.getClass().getResource("/images/test-fail-icon-disable.png"))),
            new ImageIcon(Objects.requireNonNull(
                    this.getClass().getResource("/images/test-error-icon-disable.png"))),
    };

    public TabbedPaneDisableIcon() {
        initializeUI();
    }

    public static void showFrame() {
        JPanel panel = new TabbedPaneDisableIcon();
        panel.setOpaque(true);

        JFrame frame = new JFrame("JTabbedPane Demo");
        frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
        frame.setContentPane(panel);
        frame.pack();
        frame.setVisible(true);
    }

    public static void main(String[] args) {
        SwingUtilities.invokeLater(TabbedPaneDisableIcon::showFrame);
    }

    private void initializeUI() {
        this.setLayout(new BorderLayout());
        this.setPreferredSize(new Dimension(500, 200));

        JTabbedPane pane = new JTabbedPane();

        ImageIcon tab1Icon = new ImageIcon(Objects.requireNonNull(
                this.getClass().getResource("/images/test-pass-icon.png")));
        ImageIcon tab2Icon = new ImageIcon(Objects.requireNonNull(
                this.getClass().getResource("/images/test-fail-icon.png")));
        ImageIcon tab3Icon = new ImageIcon(Objects.requireNonNull(
                this.getClass().getResource("/images/test-error-icon.png")));
        JPanel content1 = new JPanel();
        JPanel content2 = new JPanel();
        JPanel content3 = new JPanel();

        pane.addTab("Pass", tab1Icon, content1);
        pane.addTab("Fail", tab2Icon, content2);
        pane.addTab("Error", tab3Icon, content3);

        for (int i = 0; i < pane.getTabCount(); i++) {
            pane.setDisabledIconAt(i, disableIcons[i]);
        }

        // Disable the last tab to see the disabled icon displayed.
        pane.setEnabledAt(pane.getTabCount() - 1, false);

        this.add(pane, BorderLayout.CENTER);
    }
}

The result of the code snippet above is:

Set Disabled Icon for JTabbedPane Tabs

Set Disabled Icon for JTabbedPane Tabs

Wayan

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.