Click here to Skip to main content
12,949,083 members (68,909 online)
Click here to Skip to main content
Add your own
alternative version


21 bookmarked
Posted 20 Apr 2007

JTabbedPane with Closing Tabs

, 12 Aug 2010 CPOL
Rate this:
Please Sign up or sign in to vote.
This article illustrates a simple way to have closing tabs without diving into BasicTabbedPaneUI.

Screenshot - closableTabs.png


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.

// 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", 
                return choice == 0;
                // if returned false tab
                // closing will be canceled

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

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.


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


About the Author

Master of Computer and Information Science: Graduated from American University of Armenia.
Bachelor of Engineering (Management Systems): Graduated from State Engineering University of Armenia.

You may also be interested in...

Comments and Discussions

Questionconfiure size Pin
Tirolerbubble22-Jan-12 5:48
memberTirolerbubble22-Jan-12 5:48 
GeneralMy vote of 4 Pin
lazymiken11-Feb-11 21:58
memberlazymiken11-Feb-11 21:58 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

Permalink | Advertise | Privacy | Terms of Use | Mobile
Web02 | 2.8.170524.1 | Last Updated 12 Aug 2010
Article Copyright 2007 by Haykaz Baghdasaryan
Everything else Copyright © CodeProject, 1999-2017
Layout: fixed | fluid