Change the color of the selected tab


 

Change the color of the selected tab

In this section, you will learn how to change the color of the selected tab.

In this section, you will learn how to change the color of the selected tab.

Change the color of the selected tab

Swing provides different methods to give color effects which gives a presentable look to the components. In this section, you will learn how to change the color of the selected tab. For this, we have created a tabbed pane and add four tabs to it. The UIManager.put("TabbedPane.selected", Color.blue) sets the color of the selected tab. When the user clicks on the particular tab, its color will get changed. 

Here is the code:

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

public class CreateTabbedPane {
	public static void main(String[] args) {
		JFrame frame = new JFrame("Tabbed Pane Frame");
		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		UIManager.put("TabbedPane.selected", Color.blue);
		JTabbedPane tab = new JTabbedPane();
		frame.add(tab, BorderLayout.CENTER);
		tab.add("Tab 1", new JLabel("This is Tabbed Pane 1"));
		tab.add("Tab 2", new JLabel("This is Tabbed Pane 2"));
		tab.add("Tab 3", new JLabel("This is Tabbed Pane 3"));
		tab.add("Tab 4", new JLabel("This is Tabbed Pane 4"));
		frame.setSize(400, 100);
		frame.setVisible(true);
	}
}

Output:

Ads