Tutorial :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.


I believe you'll have to do it manually. Apparently it has been done before, but only as a small bit of a system which seems to not be available.

Your approach looks good to me. I would do it just like you laid it out, and wrap all that logic in a custom JComponent so it will feel less hackish.


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;        }    });    


You may be better off simply using CardLayout.


Another option would be to customize the L&F delegate (either BasicTabbedPaneUI or WindowsTabbedPaneUI depending on the platforms you care about) used by the JTabbedPane. This would allow you to customize the behavior of the tabbed pane in the case where only a single tab was being shown.

This is another way of doing things however I would say it's quite an undertaking and doing what Michael said will get you where you want to go with a lot less effort. I just wanted to post this as an answer in case you weren't aware of this option.


I think this can be achieved using tab bar and a card layout,

  • add the tab bar and card layout to a grid bag layout so that they re-size automatically
  • the max height of the tab bar should be the height of the tab
  • add a listener to tab bar so that when certain tabs are clicked it will switch the card layout to show appropriate content
  • hide the tab bar if it has only one tab

and this should do the job.


Yes, there is a way. Took me four hours to find at the oracle website: http://docs.oracle.com/javase/7/docs/api/javax/swing/JTabbedPane.html#setTabLayoutPolicy()

Simply use this:

//declare  private JTabbedPane editor = new JTabbedPane ();  //construct like this:  editor.setTabLayoutPolicy(JTabbedPane.SCROLL_TAB_LAYOUT);  //just add components and see how it goes.  editor.addTab("", newPanel);  

Note:If u also have question or solution just comment us below or mail us on toontricks1994@gmail.com
Next Post »