Setting the background color of the tabs of a JTabbedPane

You can set the background color using the method setBackgroundAt. However, this will only change the background of the tabs that are not selected. You can’t change the background of the selected tab unless you change the look and feel “TabbedPane.selected” property. This would change all JTabbedPanes that you have instantiated. A bug was filed for this: http://developer.java.sun.com/developer/bugParade/bugs/4230649.html.

import javax.swing.plaf.*;
import java.awt.event.*;
import javax.swing.*;
import java.awt.*;
 
public class Main extends JPanel {                     
  public Main() {
    setLayout(new BorderLayout());
    
    UIManager.put("TabbedPane.selected", Color.green);
    
    JTabbedPane tabbedPane = new JTabbedPane();
    for (int i=0;i<10;i++) {
      tabbedPane.addTab("Tab #" + i, new JLabel("Tab #" + i));
      tabbedPane.setBackgroundAt(i, new Color(25*i, 25*i, 25*i));
    }
    add(tabbedPane, BorderLayout.CENTER);
  }
  
  JPanel createPane(String s) {
    JPanel p = new JPanel();
    p.add(new JLabel(s));
    return p;
  }
  
  public static void main(String[] args) {
    JFrame frame = new JFrame("JTabbedPane Selected Color Demonstration");
    frame.addWindowListener( new WindowAdapter() {
      public void windowClosing( WindowEvent e ) {
        System.exit(0);
      }
    });
    frame.getContentPane().add(new Main());
    frame.setSize(200, 100);
    frame.setVisible(true);
  }
}