How can we highlight the selected tab of a JTabbedPane in Java?



In this article, we will learn to highlight the selected tab of a JTabbedPane in Java. JTabbedPane is a common Swing component that is used to organize content into multiple tabs, while highlighting a selected tab will improve the GUI and make the interface interactive.

What is a JTabbedPane?

A JTabbedPane is a subclass of the JComponent class, and it can provide easy access to more than one panel. Each tab is associated with a single component that can be displayed when the tab is selected. A JTabbedPane can generate a ChangeListener interface when a tab is selected.

What do you mean by Highlighting the Selected Tab?

Highlighting the selected tab means adding a visual distinction to the active tab relative to other tabs. Swing automatically offers simple visual differentiation, but it is possible to add more visible or customized selection indication.

How to Highlight the Selected Tab in a JTabbedPane?

We can highlight a selected tab with a particular color of a JTabbedPane by using the static method put() of the UIManager class.

put() Method

The put() is a method of the UIManager class and is used for customizing Swing components' appearance without creating a custom UI. This method takes 2 arguments as input in the form of a key and value.

Syntax

The following is the syntax for the put() method initialization:

UIManager.put("propertyKey", value);

UIManager class

UIManager determines what things look and feel like right now, the various looks and feels available to use, PropertyChangeListeners that are notified when the look and feel changes, the default look and feel, and easy ways to get various default values.

Highlighting the Selected Tab in a JTabbedPane

To highlight the selected tab of a JTabbedPane in Java, first, we will create the SelectedJTabbedPaneTest class and extend the JFrame, then we will declare a private JTabbedPane named "tabbedPane" and set the initial number of tabs to 0.

After that, we will set the frame title to "SelectedJTabbedPane Test" and position tabs using the BorderLayout. Then, using the static put() method on the UIManager class with the key as "TabbedPane.selected" and value as "Color.gray" so when a tab is selected, it highlights the selected tab as grey in color.

UIManager.put("TabbedPane.selected", Color.gray);
tabbedPane = new JTabbedPane();
createTab();

Then, initialize the JTabbedPane and create the first tab, and call the createTab() function. The createTab() function increments the tab count, then adds every new tab with the title as per the count, like "Tab 1", "Tab 2", etc, and shows the tab number using the JLabel.

public void createTab() {
   tab++;
   tabbedPane.addTab("Tab " + tab, new JLabel("Tab " + tab));
}

Example

Below is an example of highlighting the selected tab of a JTabbedPane in Java:

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class SelectedJTabbedPaneTest extends JFrame implements ActionListener {
   private JTabbedPane tabbedPane;
   int tab = 0;
   public SelectedJTabbedPaneTest() {
      setTitle("SelectedJTabbedPane Test");
      setLayout(new BorderLayout());
      UIManager.put("TabbedPane.selected", Color.gray);
      tabbedPane = new JTabbedPane();
      createTab();
      add(tabbedPane, BorderLayout.CENTER);
      setJMenuBar(createMenuBar());
      setSize(375, 250);
      setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
      setLocationRelativeTo(null);
      setVisible(true);
   }
   public JMenuBar createMenuBar() {
      JMenuBar menuBar = new JMenuBar();
      JMenu menu = new JMenu("JTabbedPane");
      JMenuItem menuItem = new JMenuItem("Create a new tab");
      menuItem.addActionListener(this);
      menu.add(menuItem);
      menuBar.add(menu);
      return menuBar;
   }
   public void actionPerformed(ActionEvent ae) {
      if (ae.getActionCommand().equals("Create a new tab")) {
         createTab();
      }
   }
   public void createTab() {
      tab++;
      tabbedPane.addTab("Tab " + tab, new JLabel("Tab " + tab));
   }
   public static void main(String []args) {
      new SelectedJTabbedPaneTest() ;
   }
}

Output

Alshifa Hasnain
Alshifa Hasnain

Converting Code to Clarity

Updated on: 2025-06-06T19:41:24+05:30

776 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements