Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles / Languages / Java / JavaSE

JTabbedPane with Closing Tabs

3.40/5 (11 votes)
12 Aug 2010CPOL2 min read 1   4K  
This article illustrates a simple way to have closing tabs without diving into BasicTabbedPaneUI.

Screenshot - closableTabs.png

Introduction

During the development of a new application, almost always, there is a need to have components with specific features in order to meet the requirements of a project or just to make things more natural and comfortable to use. Because of this, you will either extend your component from an existing one or create one from scratch. In both cases, it can bring you a lot of headache, and for a while deviate you from your main goal of development. An example of such a problem is the customization of the tabbed pane (JTabbedPane) to give it the functionality of open tabs' closing.

You can solve this problem by adding a button to the tab content, which often appears to be confronting the content, and not good looking at all, or extending the tabbed pane to have the close button embedded in the title bar of each tab. For the second choice, a comparably simple solution can be the usage of a component for tab title rendering. The component can be set for every tab (setTabComponentAt(index, component )) and you need to implement it to show the title and close button.

An alternative solution can be the usage of this extension of JTabbedPane that adds not only the functionality of closable tabs, but also gives the ability to control the event of tab closing.

Using the Code

The usage of the code is very simple; just use ClosableTabbedPane instead of JTabbedPane.

C#
// Implementation with close event handeling
public class TabFrame extends JFrame {
private ClosableTabbedPane tabbedPane;
    public TabFrame() {         
        tabbedPane = new ClosableTabbedPane() {
            public boolean tabAboutToClose(int tabIndex) {
                String tab = tabbedPane.getTabTitleAt(tabIndex);
                int choice = JOptionPane.showConfirmDialog(null, 
                   "You are about to close '" + 
                   tab + "'\nDo you want to proceed ?", 
                   "Confirmation Dialog", 
                   JOptionPane.INFORMATION_MESSAGE);
                return choice == 0;
                // if returned false tab
                // closing will be canceled
            }
        };
        getContentPane().add(tabbedPane);
        
}

// Simple implementation
public class TabFrame extends JFrame {
private ClosableTabbedPane tabbedPane;
    public TabFrame() {         
        tabbedPane = new ClosableTabbedPane() ;
        getContentPane().add(tabbedPane);
    }
}

In the first implementation, you can see that the tabAboutToClose(tabIndex) function of ClosableTabbedPane is overridden to control the event of tab closing. The returned true/false value indicates whether the tab should be closed or not. Here you can do your processing before closing the tab.

If you do not need to control the closing event, just use the second code.

Points of Interest

The interesting part of this project is that it was written without diving into the tabbed pane implementation, while achieving the desired functionality.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)