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:
But I would like to have:
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.


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: