NetBeans Platform Cookbook Chapter 10 04

Use Closeable Tabs

Each TopComponent is shown as a tab panel with cross button to allow close it. If you need such a tabs look at TabbedPaneFactory class in the UI Utilities module.


The class TabbedPaneFactory creates a JTabbedPane. Its panes have small close button on the tab. When the user clicks the button a new PropertyChangeEvent PROP_CLOSE is fired. The inner component inside the closing tab is stored in the newValue property. Usage of the component is the same as of the JTabbedPane but you can close (remove) the component from tabbed pane.

If you want disallow closing capability set component's client property NO_CLOSE_BUTTON to true.

How to

Set dependency on 'UI Utilities module.

Create component where tabs will be used. Add new JTabbedPane into it.

Open Properties of that tabbed pane, switch to code and into CustomCreationCode insert creation code:


Create an action listener which is called to close the tab.

    private class CloseListener implements PropertyChangeListener {
        public CloseListener() {     }

        public void propertyChange(PropertyChangeEvent evt) {
            if (evt.getPropertyName().equals(
                      TabbedPaneFactory.PROP_CLOSE))  {
                JComponent closedComponent = (JComponent)
                // if can close
                tabPane.remove( closedComponent );
                // other code 
    } // CloseListener
    private PropertyChangeListener closeListener = new CloseListener();

and register it in the constructor after initialization

     tabPane.addPropertyChangeListener( closeListener );

Create new tab as usual:

    String name = "tab " + ++tab_num;
    JPanel panel = new InnPanel( name ); ...
    if ( ! allowClosingChk.isSelected() ) {  // closing is not allowed
                TabbedPaneFactory.NO_CLOSE_BUTTON, Boolean.TRUE);
    tabPane.addTab(name, panel);   


Figure 10.7 Closeable tabs

Notes/Tips - documentation

Documentation links:

TabbedPaneFactory class

UI Utilities API is on .

Notes/Tips - examples

Try component behaviour in the 10_best / CloseTabbedPane directory of example sources.

Text and sources were created under NB 6.8, 6.9, 7.0 and 7.1.


Not logged in. Log in, Register

By use of this website, you agree to the NetBeans Policies and Terms of Use. © 2012, Oracle Corporation and/or its affiliates. Sponsored by Oracle logo