How do I disable or enable tabs in JTabbedPane?

To enable or disable tabs in JTabbedPane you can use the JTabbedPane‘s setEnableAt(int index, boolean enable) method. The index is zero based, this means that the first tab is at index number 0. The enable parameter when set to true will enable the tab while setting to false will disable the tab.

package org.kodejava.example.swing;

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

public class TabbedPaneEnableDisableTab extends JPanel {
    public TabbedPaneEnableDisableTab() {
        initializeUI();
    }

    private void initializeUI() {
        JTabbedPane pane = new JTabbedPane();
        pane.addTab("Tabs A", new JPanel());
        pane.addTab("Tabs B", new JPanel());
        pane.addTab("Tabs C", new JPanel());
        pane.addTab("Tabs D", new JPanel());

        //
        // Disable the first tab
        //
        pane.setEnabledAt(0, false);

        //
        // Disable the last tab, the pane.getTabCount() return the
        // number of tabs in the JTabbedPane. Because the index
        // start at 0 we need to subtract the count by 1.
        //
        pane.setEnabledAt(pane.getTabCount() - 1, false);

        this.setLayout(new BorderLayout());
        this.setPreferredSize(new Dimension(400, 200));
        this.add(pane, BorderLayout.CENTER);
    }

    public static void showFrame() {
        JPanel panel = new TabbedPaneEnableDisableTab();
        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(new Runnable() {
            public void run() {
                TabbedPaneEnableDisableTab.showFrame();
            }
        });
    }
}

Below is the screenshot result of the code snippet above:

JTabbedPane Disable Enable Tab

How do I add icon to JTabbedPane tabs?

The following code snippet demonstrates how to create a JTabbedPane component with image icons attached on their tabs. There are two steps that we need to do to achieve this. First we need to load the image icon and after that we need to attach these icons to the tabs.

To create or load an image icon simply create an instance of ImageIcon class. Pass the information about the image location to the constructor of the ImageIcon. In the example below I provide the location of the image using the getClass().getResource() method which will load the images from resources directory.

To add a new tab with image icon attached to the tabs of a JTabbedPane component we are using the addTab(String, Icon, Component) method. The second argument in this method is the image icon of the tab.

Let’s see the code snippet below:

package org.kodejava.example.swing;

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

public class TabbedPaneWithIcon extends JPanel {
    private TabbedPaneWithIcon() {
        initializeUI();
    }

    private static void showFrame() {
        JPanel panel = new TabbedPaneWithIcon();
        panel.setOpaque(true);

        JFrame frame = new JFrame("Tabbed Pane With Icon Demo");
        frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
        frame.setContentPane(panel);
        frame.pack();
        frame.setVisible(true);
    }

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

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

        JTabbedPane pane = new JTabbedPane();

        ImageIcon tab1Icon = new ImageIcon(
            this.getClass().getResource("/images/test-pass-icon.png"));
        ImageIcon tab2Icon = new ImageIcon(
            this.getClass().getResource("/images/test-fail-icon.png"));
        ImageIcon tab3Icon = new ImageIcon(
            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);

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

The directory structure of the project:

kodejava-swing
├── pom.xml
└── src
    └── main
        ├── java
        │   └── org
        │       └── kodejava
        │           └── example
        │               └── swing
        │                   └── TabbedPaneWithIcon.java
        └── resources
            └── images
                ├── test-error-icon.png
                ├── test-fail-icon.png
                └── test-pass-icon.png

And here is the result of the code snippet above.

TabbedPane Tab With Icon

TabbedPane Tab With Icon

How do I scroll or wrap JTabbedPane tab layout?

JTabbedPane component tab layout policy of can be either set to JTabbedPane.SCROLL_TAB_LAYOUT or JTabbedPane.WRAP_TAB_LAYOUT. This layout policy will control the display of the tabs when the tabs cannot be displayed in one go. By the default the layout policy is set to JTabbedPane.WRAP_TAB_LAYOUT. Changing tab layout policy to JTabbedPane.SCROLL_TAB_LAYOUT makes the tabs scrollable. A button for scrolling left-right or up-down will be displayed in the tabbed pane.

package org.kodejava.examples.swing.tabbedpane;

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

public class TabbedPaneTabLayoutPolicy extends JPanel {
    public TabbedPaneTabLayoutPolicy() {
        initializeUI();
    }

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

        JFrame frame = new JFrame("TabbedPane Tab Layout Policy Demo");
        frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
        frame.setContentPane(panel);
        frame.pack();
        frame.setVisible(true);
    }

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

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

        // Creates a JTabbedPane with scroll tab layout policy
        JTabbedPane pane = new JTabbedPane(JTabbedPane.TOP, JTabbedPane.SCROLL_TAB_LAYOUT);
        pane.addTab("One", createPanel("One"));
        pane.addTab("Two", createPanel("Two"));
        pane.addTab("Three", createPanel("Three"));
        pane.addTab("Four", createPanel("Four"));
        pane.addTab("Five", createPanel("Five"));
        pane.addTab("Six", createPanel("Six"));
        pane.addTab("Seven", createPanel("Seven"));
        pane.addTab("Eight", createPanel("Eight"));
        pane.addTab("Nine", createPanel("Nine"));
        pane.addTab("Ten", createPanel("Ten"));

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

    public JPanel createPanel(String title) {
        JPanel panel = new JPanel();
        panel.setLayout(new BorderLayout());
        panel.add(new JLabel(title), BorderLayout.NORTH);
        return panel;
    }
}

Here is the screenshot of the code snippet created above. You can see that a button at the top right of the tabbed pane is added so that you can scroll to navigate the hidden tabs.

Tabbed Pane Tab Layout Policy

Tabbed Pane Tab Layout Policy