How to Scroll Tabs in JTabbedPane?

You might have seen that tabs in JTabbedPane don't scroll. If more tabs are added into the JTabbedPane, then tabs are added to the bottom if the tabs aren't fit in a single row.

Multiple rows are messy, right? So, wouldn't a scroll layout be better just like in the CCleaner? Fine, when you ask me Do i need to hack? I say, NO. There is a method, yes, here it is.

public void setTabLayoutPolicy(int policy);

This method takes either of these two values, SCROLL_TAB_LAYOUT and WRAP_TAB_LAYOUT which have 1 and 0 values respectively. The first one is what does the thing. Let us see it in action.

import javax.swing.*;
import java.awt.*;
class JTabbedPaneScroll extends JFrame
JTabbedPane t;

    public JTabbedPaneScroll()
    private void createAndShowGUI()
        setTitle("JTabbedPane Scroll");
        t=new JTabbedPane();
        for(int i=1;i<=10;i++){
        JPanel p=new JPanel();
        p.setBackground(new Color(i,i*8,i*4));
        t.addTab("Tab "+i,p);
        // Now make it scrollable
        // or you can call
        // t.setTabLayoutPolicy(JTabbedPane.SCROLL_TAB_LAYOUT);
    public static void main(String args[])
        new JTabbedPaneScroll();

The greatest compliment you can give me is when you share this with others. I sincerely appreciate it :)


Posted by at on

Tags: Swing Components,