[ACCEPTED]-Is there a way to hide the tab bar of JTabbedPane if only one tab exists?-jtabbedpane

Accepted answer
Score: 13

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

tabbed_pane.setUI(new BasicTabbedPaneUI() {  
    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);  
            return 0;  
Score: 6

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

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

Score: 6

You may be better off simply using CardLayout.


Score: 1

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

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

Score: 1

I think this can be achieved using tab bar 2 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 1 job.

Score: 1

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

Simply use 1 this:

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

jTabbedPane1.removeTabAt(0); seems to work after initComponents();


More Related questions