Click here to Skip to main content
15,885,032 members
Articles / Programming Languages / Java / Java SE

JTabbedPane with Closing Tabs

Rate me:
Please Sign up or sign in to vote.
3.40/5 (11 votes)
12 Aug 2010CPOL2 min read 96.9K   4K   21   3
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)


Written By
Canada Canada
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

 
QuestionHow to trigger the if event is running before close the tabbedpane Pin
jagadeesan.in12-Jun-18 19:51
jagadeesan.in12-Jun-18 19:51 
Questionconfiure size Pin
Tirolerbubble22-Jan-12 5:48
Tirolerbubble22-Jan-12 5:48 
GeneralMy vote of 4 Pin
lazymiken11-Feb-11 21:58
lazymiken11-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.