- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
How can we highlight the selected tab of a JTabbedPane in Java?
A JTabbedPane is a subclass of 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. We can highlight a selected tab with a particular color of a JTabbedPane by using the static method put() of the UIManager class.
Example
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); // set the color of selected tab to 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
- Related Articles
- How to add a Tab in JTabbedPane with Java?
- How to disable a Tab in a JTabbedPane Container with Java?
- Insert a tab after the first tab of a JTabbedPane container
- How can we change the default font of the JTabbedPane tabs in Java?
- How to set the background color of a single tab in a JTabbedPane Container with Java?
- How can we insert multiple tabs into a single JTabbedPane in Java?
- How can we add new tabs to JTabbedPane from a JMenu in Java?
- How can we remove a selected row from a JTable in Java?
- How to properly highlight selected item on Android RecyclerView?
- How to customize how a JTabbedPane looks in Java?
- How can we get the current language selected in the Android device?
- How to properly highlight selected items on Android RecyclerView using Kotlin?
- How to get the Tab Size of a JTextArea in Java?
- How can we get the current language selected in the Android device using Kotlin?
- How can we sort the items of a JComboBox in Java?

Advertisements