java examples
Home Java Examples Resources Java Interview Questions
Brainpower SEO

How to 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 meas the first tab is at index number 0. The enable parameter when set to true will enable the tabs while setting to false will disable the tabs.

package com.javacoderanch.example.swing;

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

public class TabbedPaneEnableDisableTab extends JPanel {
    public TabbedPaneEnableDisableTab() {

    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 substract 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();

        JFrame frame = new JFrame("JTabbedPane Demo");

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