How to change the background and foreground colors of tab in Java?


To change the background and foreground colors of tab, use the following methods −

JTabbedPane tabbedPane = new JTabbedPane();
tabbedPane.setBackground(Color.blue);
tabbedPane.setForeground(Color.white);

Above, we have used the Color class to set the colors for the background and foregroud colors −

The following is an example to change the background and foreground colors of tab −

package my;
import javax.swing.*;
import java.awt.*;
import java.awt.event.KeyEvent;
public class SwingDemo {
   public static void main(String args[]) {
      JFrame frame = new JFrame("Devices");
      JTabbedPane tabbedPane = new JTabbedPane();
      JPanel panel1, panel2, panel3, panel4, panel5;
      panel1 = new JPanel();
      panel2 = new JPanel();
      panel3 = new JPanel();
      panel4 = new JPanel();
      panel5 = new JPanel();
      tabbedPane.setBackground(Color.blue);
      tabbedPane.setForeground(Color.white);
      tabbedPane.addTab("Laptop", panel1);
      tabbedPane.addTab("Desktop ", panel2);
      tabbedPane.addTab("Notebook", panel3);
      tabbedPane.addTab("Tablet ", panel4);
      tabbedPane.addTab("Mobile", panel5);
      frame.add(tabbedPane);
      frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
      frame.setSize(550,350);
      frame.setVisible(true);
   }
}

Output

Updated on: 30-Jul-2019

483 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements