BookNBPlatformCookbookCH1004

Contents

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.

Preparation

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:

   TabbedPaneFactory.createCloseButtonTabbedPane()

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)
                                       evt.getNewValue(); 
                // 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
        panel.putClientProperty(
                TabbedPaneFactory.NO_CLOSE_BUTTON, Boolean.TRUE);
    }
    
    tabPane.addTab(name, panel);   

image:Nbpcook_10_07_closetabbedpane.png

Figure 10.7 Closeable tabs

Notes/Tips - documentation

Documentation links:

TabbedPaneFactory class http://bits.netbeans.org/dev/javadoc/org-openide-awt/org/openide/awt/TabbedPaneFactory.html.

UI Utilities API is on http://bits.netbeans.org/dev/javadoc/org-openide-awt/overview-summary.html .

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.

Navigation

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