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

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

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

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 may be better off simply using CardLayout.