Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there a way to hide the tab bar of JTabbedPane if only one tab exists?

I want a behavior similar to e.g. Firefox where the list of available tabs does only show up if at least two tabs exist.

I wasn't able to find anything like that, yet.

The best idea I had was changing the layout manually:

  • in case of one component, just add that to the surrounding panel
  • if a component is added, remove the component from the surrounding panel, add a JTabbedPane instead and add both the previous and the new component to that pane.
  • if a component is removed and only one component is left in the pane, remove the pane and add the contained component instead.

While this would probably work it feels like a hack or workaround...

Any better idea?

A solution should ideally work in both Java 1.5 and 1.6... but I'd be happy about a 1.6-only solution, too.

like image 421
Huxi Avatar asked Jun 03 '09 00:06

Huxi


People also ask

How do I hide tabs in JTabbedPane?

To disable a tab in a JTabbedPane container, use the setEnabledAt() method and set it to false with the index of the tab you want to disable.

What is display by J tabbed pane?

What is JTabbedPane? JTabbedPane is one of the classes provided by the swing package in java. It is very useful, as it provides the flexibility to the user to switch between different groups of components he desires to see, by simply clicking on one of the tabs. The tabs can be given different titles or icons.

How do you switch tabs in JTabbedPane by clicking a button?

You should use the method JTabbedPane. setSelectedIndex(int index) with the index of the tab you want. Show activity on this post. JTabbedpane.

Which method is used to add tabs to a JTabbedPane?

To create a tabbed pane, instantiate JTabbedPane , create the components you wish it to display, and then add the components to the tabbed pane using the addTab method.


1 Answers

You can override the UI method that calculates the height for the tab button area, forcing the height to 0 when there's only one tab:

tabbed_pane.setUI(new BasicTabbedPaneUI() {  
    @Override  
    protected int calculateTabAreaHeight(int tab_placement, int run_count, int max_tab_height) {  
        if (tabbed_pane.getTabCount() > 1)
            return super.calculateTabAreaHeight(tab_placement, run_count, max_tab_height);  
        else  
            return 0;  
    }  
});  
like image 112
Brad Mace Avatar answered Oct 20 '22 17:10

Brad Mace