How do I remove tabs from JTabbedPane?

To remove tabs from JTabbedPane we can use the following remove methods:

  • remove(int index) method to remove a tab at the specified index.
  • remove(Component component) method to remove a tab that has the specified child component.
  • removeAll() method to remove all tabs.
package org.kodejava.swing;

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

public class TabbedPaneRemoveTab extends JPanel {
    public TabbedPaneRemoveTab() {

    public static void showFrame() {
        JPanel panel = new TabbedPaneRemoveTab();

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

    public static void main(String[] args) {

    private void initializeUI() {
        final JTabbedPane pane = new JTabbedPane(JTabbedPane.LEFT);
        pane.addTab("A Tab", new JPanel());
        pane.addTab("B Tab", new JPanel());

        JPanel tabPanel = new JPanel();
        pane.addTab("C Tab", tabPanel);
        pane.addTab("D Tab", new JPanel());
        pane.addTab("E Tab", new JPanel());

        // Remove the last tab from JTabbedPane
        pane.remove(pane.getTabCount() - 1);

        // Remove tab that contains a tabPanel component which is
        // the C Tab.

        JButton button = new JButton("Remove All Tabs");
        button.addActionListener(e -> {
            // Remove all tabs from JTabbedPane

        this.setLayout(new BorderLayout());
        this.setPreferredSize(new Dimension(500, 200));
        this.add(pane, BorderLayout.CENTER);
        this.add(button, BorderLayout.SOUTH);

The output of the code snippet above is:

Remove Tabs from JTabbedPane

Leave a Reply

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