javaswingjtabbedpane

Is there a way to have all the tabs in a JTabbedPane on one row, without having a scrollbar


I have a JTabbedPane where I want tabs in only one row, but without having a scroll arrow. I want the tabs to fill the width of the parent JFrame.

I have the following code:

public class TabbedPaneExample extends JFrame {
   public TabbedPaneExample() {
      super("My TabbedPane");
      setup();
   }

   private void setup() {
      JTabbedPane tabbedPane = new JTabbedPane();
      tabbedPane.add("The first tab", new JPanel());
      tabbedPane.add("The second tab", new JPanel());
      tabbedPane.add("The third tab", new JPanel());
      tabbedPane.add("The fourth tab", new JPanel());
      tabbedPane.add("The fith tab", new JPanel());
      tabbedPane.setTabLayoutPolicy(JTabbedPane.SCROLL_TAB_LAYOUT);

      Container pane = this.getContentPane();
      pane.setLayout(new BorderLayout());
      this.add(tabbedPane, BorderLayout.CENTER);
   }

   public static void main(String[] args) {
      TabbedPaneExample example = new TabbedPaneExample();
      example.pack();
      example.setVisible(true);
   }
}

I have the following result when I start my program:

The tabbed pane at start

But I would like to have:

What I want

I only found the setTabLayoutPolicy method, but ideally I would like to get the preferred size of the tabbed pane and use it in my frame.


Solution

  • ideally I would like to get the preferred size of the tabbed pane

    Yes, that is the key. The default preferred size is based on the size of the largest component added to the tabbed pane, not the tabs themselves.

    So you need to customize the getPreferredSize() method to consider the tabs:

    import java.awt.*;
    import javax.swing.*;
    import javax.swing.plaf.*;
    
    public class TabbedPaneExample2
    {
        private static void createAndShowGUI()
        {
            JTabbedPane tabbedPane = new JTabbedPane()
            {
                //@Override
                public Dimension getPreferredSize()
                {
                    Dimension size = super.getPreferredSize();
    
                    TabbedPaneUI ui = getUI();
                    Rectangle lastTab = ui.getTabBounds(this, this.getTabCount() - 1);
                    int tabsWidth = lastTab.x + lastTab.width;
    
                    Insets border = UIManager.getInsets("TabbedPane.contentBorderInsets");
                    tabsWidth += border.left + border.right;
    
                    if (tabsWidth > size.width)
                        size.width = tabsWidth;
    
                    return size;
                }
            };
            tabbedPane.setTabLayoutPolicy(JTabbedPane.SCROLL_TAB_LAYOUT);
    
            JPanel first = new JPanel();
            first.setPreferredSize( new Dimension(400, 400) );
            tabbedPane.add("The first tab", first);
            tabbedPane.add("The second tab", new JPanel());
            tabbedPane.add("The third tab has longer text", new JPanel());
            tabbedPane.add("The fourth tab", new JPanel());
            tabbedPane.add("The fith tab", new JPanel());
    
            JFrame frame = new JFrame("TabbedPaneExample2");
            frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            frame.add(tabbedPane);
            frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
            frame.pack();
            frame.setLocationByPlatform( true );
            frame.setVisible( true );
        }
    
        public static void main(String[] args)
        {
            java.awt.EventQueue.invokeLater( () -> createAndShowGUI() );
        }
    }