5,662,937 members and growing! (105 online)
Email Password   helpLost your password?
Desktop Development » Tabs & Property Pages » General     Intermediate

JTabbedPane with Closing Tabs

By Haykaz Baghdasaryan

This article illustrates a simple way to have closing tabs without diving into BasicTabbedPaneUI
Javascript, Java, Java, Dev

Posted: 20 Apr 2007
Updated: 20 Apr 2007
Views: 9,977
Bookmarked: 6 times
Announcements
Loading...



Search    
Advanced Search
Sitemap
5 votes for this Article.
Popularity: 2.05 Rating: 2.93 out of 5
1 vote, 20.0%
1
1 vote, 20.0%
2
0 votes, 0.0%
3
1 vote, 20.0%
4
2 votes, 40.0%
5

Screenshot - closeTabs.jpg

Introduction

Sometimes during development of a project a need arises - components, by their default implementation, do not have the functionality to meet the requirements of the project. Because of this fact, developers have to extend given components in order to bring them into a form which will match their needs. An example of such a problem that occurs quite often is the customization of the tabbed pane (JTabbedPane) to give it a functionality of closing open tabs.

As a result, you have to extend the BasicTabbedPaneUI and set it to your custom tabbed pane in order to embed a close button into each open tab. Naturally, you will not choose the simple solution of just adding them into their panes (though sometimes you have to since the creation of your own tabbed pane creates additional problems, like you having to override too many methods in order to get the functionality you want).

Using the code

You can avoid such a problem by using another quite simple solution, compared to an overall custom tabbed pane creation. All you have to do is add a listener to the standard tabbed pane, extended from the JTabbedPane, but with the ability to draw itself embedded into an open tab. This simple trick will make your life easier while having a good-looking close button. For this purpose, all we need is to pass the Graphics of the tabbed pane to the listener in order to give it the ability to draw itself. We do not need to override any method of JTabbedPane besides paint, through which we will get needed functionality. Here is the all code for the extended tabbed pane.

public class CloseTabbedPane extends JTabbedPane{
    private TabCloseUI closeUI;
    public CloseTabbedPane(){            
        addTab("TAB 1 ", new JPanel());
        closeUI = new TabCloseUI();
        closeUI.setTabbedPane(this);
        addMouseMotionListener(closeUI);
        addMouseListener(closeUI);
    }
    
    public void paint(Graphics g){
        super.paint(g);
        closeUI.paint(g);
    }
}

Here you can see that in order to give your tabbed pane closing capability all you need to do is just use TabCloseUI (enclosed in this article source) as MouseMotionListener and MouseListener which interfaces it, implements it, and then just overrides the paint method of JTabbedPane.

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 has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here

About the Author

Haykaz Baghdasaryan


Got Bachelor degree of Engineering from State Engineering University of Armenia.
Currently at American University of Armenia. After graduation will get Master's degree of Computer and Information Science.
Occupation: Web Developer
Location: Armenia Armenia

Other popular Tabs & Property Pages articles:

Article Top
Sign Up to vote for this article
You must Sign In to use this message board.
FAQ FAQ Noise ToleranceSearch Search Messages 
 Layout  Per page   
  (Refresh) 
-- There are no messages in this forum --

General General    News News    Question Question    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

PermaLink | Privacy | Terms of Use
Last Updated: 20 Apr 2007
Editor: Sean Ewington
Copyright 2007 by Haykaz Baghdasaryan
Everything else Copyright © CodeProject, 1999-2008
Java | Advertise on the Code Project