How to set the background color of a single tab in a JTabbedPane Container with Java?

To set the background color of a single tab, use the setBackgroundAt() method. This gives an option to mention the index and the color. The index here is the index of the specific tab you want to color.

Let us first create a JTabbedPane −

JTabbedPane tabbedPane = new JTabbedPane();

Now, set the background color for one of the tabs with index 2 −

tabbedPane.setBackgroundAt(2, Color.RED);

The following is an example to set the background color of a single tab in a JTabbedPane container −


package my;
import javax.swing.*;
import java.awt.*;
public class SwingDemo {
   public static void main(String args[]) {
      JFrame frame = new JFrame("Devices");
      JTabbedPane tabbedPane = new JTabbedPane();
      JTextArea text = new JTextArea(100,100);
      JPanel panel1, panel2, panel3, panel4, panel5, panel6, panel7, panel8;
      panel1 = new JPanel();
      panel2 = new JPanel();
      panel3 = new JPanel();
      panel4 = new JPanel();
      panel5 = new JPanel();
      panel6 = new JPanel();
      panel7 = new JPanel();
      panel8 = new JPanel();
      tabbedPane.addTab("Laptop", panel1);
      tabbedPane.addTab("Desktop ", panel2);
      tabbedPane.addTab("Notebook", panel3);
      tabbedPane.addTab("Echo ", panel4);
      tabbedPane.addTab("Tablet", panel5);
      tabbedPane.addTab("Alexa ", panel6);
      tabbedPane.addTab("Notebook", panel7);
      tabbedPane.addTab("iPad", panel8);
      tabbedPane.setBackgroundAt(2, Color.RED);

The output is as follows. Here, we have changed the background color to RED of a single tab with index 2 −