Click here to Skip to main content
Click here to Skip to main content

JTabbedPane with Closing Tabs

, 12 Aug 2010
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

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.

// 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)

Share

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.

Comments and Discussions

 
Questionconfiure size PinmemberTirolerbubble22-Jan-12 5:48 
GeneralMy vote of 4 Pinmemberlazymiken11-Feb-11 21:58 

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

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

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