Java Swing How to - Add Spacing Between JTabbedPane








Question

We would like to know how to add Spacing Between JTabbedPane.

Answer

import java.awt.LayoutManager;
//w  w w.  j av  a 2  s .  com
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JTabbedPane;
import javax.swing.plaf.basic.BasicTabbedPaneUI;

public class Main {
  public static void main(String[] args) throws Exception {
    JTabbedPane pane = new JTabbedPane();
    pane.setUI(new SpacedTabbedPaneUI());
    pane.addTab("One", new JPanel());
    pane.addTab("Two", new JPanel());
    pane.addTab("Threeeeeee", new JPanel());
    pane.addTab("Four", new JPanel());

    JFrame frame = new JFrame();
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.add(pane);
    frame.setSize(500, 200);
    frame.setLocationRelativeTo(null);
    frame.setVisible(true);
  }
}
class SpacedTabbedPaneUI extends BasicTabbedPaneUI {
  @Override
  protected LayoutManager createLayoutManager() {
    return new BasicTabbedPaneUI.TabbedPaneLayout() {
      @Override
      protected void calculateTabRects(int tabPlacement, int tabCount) {
        final int spacer = 20; // should be non-negative
        final int indent = 4;

        super.calculateTabRects(tabPlacement, tabCount);

        for (int i = 0; i < rects.length; i++) {
          rects[i].x += i * spacer + indent;
        }
      }
    };
  }
}