How do I set the colors of JTabbedPane tabs?

The example below demonstrates how to change the color of the tabs in JTabbedPane component. The JTabbedPane‘s methods that you can use the change foreground and background color are:

  • setForeground(Color color) for changing the foreground color of all tabs
  • setBackground(Color color) for changing the background color of all tabs
  • setForegroundAt(int index, Color color) for changing foreground color for a tab at defined index
  • setBackgroundAt(int index, Color color) for changing the background color of a tab at a defined index.

 

package org.kodejava.example.swing;

import javax.swing.*;
import java.awt.*;
import java.awt.event.KeyEvent;

public class TabbedPaneTabColor extends JPanel {
    public TabbedPaneTabColor() {
        initializeUI();
    }

    private void initializeUI() {
        this.setLayout(new BorderLayout());
        this.setPreferredSize(new Dimension(400, 200));

        JTabbedPane pane = new JTabbedPane();
        pane.addTab("A Tab", new JPanel());
        pane.addTab("B Tab", new JPanel());
        pane.addTab("C Tab", new JPanel());
        pane.addTab("D Tab", new JPanel());

        //
        // Set all tabs foreground color to black.
        //
        pane.setForeground(Color.BLACK);
        
        //
        // Set different background color for all tabs in 
        // JTabbedPane. The color from the first to the last
        // tab will be red, green yellow and orange.
        //
        pane.setBackgroundAt(0, Color.RED);
        pane.setBackgroundAt(1, Color.GREEN);
        pane.setBackgroundAt(2, Color.YELLOW);
        pane.setBackgroundAt(3, Color.ORANGE);

        this.add(pane, BorderLayout.CENTER);
    }

    public static void showFrame() {
        JPanel panel = new TabbedPaneTabColor();
        panel.setOpaque(true);

        JFrame frame = new JFrame("JTabbedPane Demo");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setContentPane(panel);
        frame.pack();
        frame.setVisible(true);
    }

    public static void main(String[] args) {
        SwingUtilities.invokeLater(new Runnable() {
            public void run() {
                TabbedPaneTabColor.showFrame();
            }
        });
    }
}
Wayan Saryada

Wayan Saryada

A programmer, runner, recreational diver, currently living in the island of Bali, Indonesia. Mostly programming in Java, creating web based application with Spring Framework, JPA, etc. If you need help on Java programming you can hire me on Fiverr.
Wayan Saryada

Leave a Reply