Java JTabbedPane Last Updated : 28 Apr, 2025 Summarize Comments Improve Suggest changes Share Like Article Like Report JTabbedPane is a GUI(Graphical User Interface) component in the Java Swing library that allows you to create a tabbed pane interface. A tabbed pane is a container that can store and organize multiple components into distinct tabs. It contains a collection of tabs. When you click on a tab, only data related to that tab will be displayed. JTabbedPane comes under the Java Swing package. Key Features of JTabbedPaneTabs: Each tab in a JTabbedPane is normally represented by a title or icon, and clicking on a particular tab shows the information only related to that tab.Tab Placement: JTabbedPane provides many tab placement options, such as top, bottom, left, or right sides of the pane, allowing you to customize the layout.Scrolling: If the tabs don't fit in the visible region, JTabbedPane provides scrolling options to navigate through them. Event Handling: You can add event listeners to JTabbedPane to respond to tab selection or deselection events, allowing you to do actions when the user switches between tabs.Constructors used in JTabbedPane Constructor Description JTabbedPane() The JTabbedPane class in Java Swing has a default, no-argument constructor called JTabbedPane(). This constructor initializes an empty tabbed pane with no tabs and no initial content when a JTabbedPane is created. JTabbedPane(int tabPlacement) The JTabbedPane(int tabPlacement) constructor allows to creation of a JTabbedPane with a defined initial location for the tabs. The tab placement option specifies whether the tabs appear at the top, bottom, left, or right of the tabbed pane. Some commonly used methods of the JTabbedPane Method Description addTab(String title, Component component) Creates a new tab with the given title and content. removeTabAt(int index) Removes the tab at the given index. getTabCount() Returns the number of tabs present in the JTabbedPane. setSelectedIndex(int index) Sets the chosen tab to the index given. getSelectedIndex() Returns the index of the currently selected tab. The classes from which JTabbedPane methods are inheritedjava.awt.Containerjavax.swing.JComponentjavax.swing.JTabbedPanejavax.swing.JContainerPrograms to implement JTabbedPaneExample 1: JTabbedPane with LabelsBelow is the implementation of the JTabbedPane: Java // Java Program to demonstrate // JTabbedPane with Labels import javax.swing.*; import java.awt.*; // Driver Class public class TabbedUIExample1 { // main function public static void main(String[] args) { // Run the Swing application on the Event Dispatch Thread (EDT) SwingUtilities.invokeLater(new Runnable() { public void run() { // Create a new JFrame (window) JFrame window = new JFrame("JTabbedPane Example"); // Close operation when the window is closed window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); // Close operation when the window is closed // Set the initial size of the window window.setSize(400, 300); // Create a JTabbedPane, which will hold the tabs JTabbedPane tabPanel = new JTabbedPane(); // Create the first tab (page1) and add a JLabel to it JPanel page1 = new JPanel(); page1.add(new JLabel("This is Tab 1")); // Create the second tab (page2) and add a JLabel to it JPanel page2 = new JPanel(); page2.add(new JLabel("This is Tab 2")); // Create the third tab (page3) and add a JLabel to it JPanel page3 = new JPanel(); page3.add(new JLabel("This is Tab 3")); // Add the three tabs to the JTabbedPane tabPanel.addTab("Tab 1", page1); tabPanel.addTab("Tab 2", page2); tabPanel.addTab("Tab 3", page3); // Add the JTabbedPane to the JFrame's content window.add(tabPanel); // Make the JFrame visible window.setVisible(true); } }); } } Output:Example 2: Dynamically add and remove tabs from a JTabbedPaneBelow is the implementation of JTabbedPane: Java // Java AWT Program to implementaion // Dynamically add and remove tabs from a JTabbedPane import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import javax.swing.*; // Driver Class public class TabbedUIExample2 { // main function public static void main(String[] args) { // Create the main JFrame window JFrame window = new JFrame( "This is an Example of Dynamic Tab"); // Set the close operation window.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE); // Set the initial size of the window window.setSize( 450, 350); // Set the initial size of the window // Create a JTabbedPane to manage tabs JTabbedPane tabPanel = new JTabbedPane(); // Create an "Add Tab" button with an ActionListener JButton addTabButton = new JButton("Add Tab"); addTabButton.addActionListener( new ActionListener() { @Override public void actionPerformed(ActionEvent e) { // Create a new JPanel for the added tab JPanel newTab = new JPanel(); newTab.add(new JLabel( "New Tab Added")); // Add a label to // the new tab // Add the new tab to the JTabbedPane // with a dynamically generated title tabPanel.addTab( "Tab " + tabPanel.getTabCount(), newTab); } }); // Create a "Remove Tab" button with an // ActionListener JButton removeTabButton = new JButton("Remove Tab"); removeTabButton.addActionListener( new ActionListener() { @Override public void actionPerformed(ActionEvent e) { // Get the index of the currently // selected tab int selectedIndex = tabPanel.getSelectedIndex(); if (selectedIndex != -1) { // Remove the selected tab tabPanel.removeTabAt(selectedIndex); } } }); // Create a control panel to hold the buttons JPanel controlPanel = new JPanel(); // Add "Add Tab" button controlPanel.add(addTabButton); // Add "Remove Tab" button controlPanel.add(removeTabButton); // Add the JTabbedPane to the JFrame's content in // the center and the control panel at the bottom window.add(tabPanel, BorderLayout.CENTER); window.add(controlPanel, BorderLayout.SOUTH); // Make the JFrame visible window.setVisible(true); } } Output: Comment More infoAdvertise with us Next Article Java JOptionPane B biswalsh6n1t Follow Improve Article Tags : Java Geeks Premier League java-swing Geeks Premier League 2023 Practice Tags : Java Similar Reads Java JTextPane In Java, JTextPane is a versatile component that is part of the Swing library for building graphical user interfaces. It extends JEditorPane and provides an editable text component with rich text formatting capabilities. JTextPane allows you to display and edit styled text, making it suitable for im 3 min read Java JLayeredPane In Java Swing, JLayeredPane is a powerful container that allows you to overlay and manage multiple components within a single container. Each component in a JLayeredPane can occupy a different layer, and you can control their positions and visibility. In this article, we are going to implement the J 4 min read Java JEditorPane A JEditorPane is a component in Java Swing that provides a simple way to display and edit HTML and RTF (Rich Text Format) content. It is a versatile component that allows you to display styled text, links, and images, making it useful for creating simple web browsers, rich-text editors, and content 3 min read Java JRootPane In Java Swing, JRootPane is a fundamental component that serves as the root container for creating complex user interfaces. It encapsulates the core functionality of a top-level window, like JFrame, and provides a structure for organizing the content within a GUI application. In Java JRootPane is a 3 min read Java JOptionPane In Java, JOptionPane is a part of the Java Swing library. It helps us to create dialog boxes such as message dialogs, conformation dialogs, input dialogs, and options dialogs In this article, we are going to explore some constructors, methods, and some examples of JOptionPane. Constructors of JOptio 5 min read Java JTree The JTree is a type of GUI(Graphic User Interface) that displays information in a hierarchical way. This intricate component part provides a quite elegant substance of representing relationships among elements in a tree-like structure. In this exploration, we'll delve into the essence of the JTree c 2 min read Like