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.example.swing;

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

public class TabbedPaneDisableIcon extends JPanel {
    private ImageIcon[] disableIcons = {
        new ImageIcon(this.getClass().getResource("/dis1.png")),
        new ImageIcon(this.getClass().getResource("/dis2.png")),
        new ImageIcon(this.getClass().getResource("/dis3.png")),        
    };

    public TabbedPaneDisableIcon() {
        initializeUI();
    }

    private void initializeUI() {
        this.setLayout(new BorderLayout());
        this.setPreferredSize(new Dimension(400, 100));

        JTabbedPane pane = new JTabbedPane();

        ImageIcon tab1Icon = new ImageIcon(
                this.getClass().getResource("/tab1.png"));
        ImageIcon tab2Icon = new ImageIcon(
                this.getClass().getResource("/tab2.png"));
        ImageIcon tab3Icon = new ImageIcon(
                this.getClass().getResource("/tab3.png"));

        JPanel content1 = new JPanel();
        JPanel content2 = new JPanel();
        JPanel content3 = new JPanel();

        pane.addTab("Pool", tab1Icon, content1);
        pane.addTab("Basketball", tab2Icon, content2);
        pane.addTab("Football", 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);
    }

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

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

    public static void main(String[] args) {
        SwingUtilities.invokeLater(new Runnable() {
            public void run() {
                TabbedPaneDisableIcon.showFrame();
            }
        });
    }
}
Wayan Saryada

Wayan Saryada

A programmer, runner, recreational diver, currently living in the island of Bali, Indonesia. Mostly programming in Java, creating web based application with Spring Framework, JPA, etc. If you need help on Java programming you can hire me on Fiverr.
Wayan Saryada

Leave a Reply