Open In App

Java JTabbedPane

Last Updated : 07 Nov, 2023
Improve
Improve
Like Article
Like
Save
Share
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 JTabbedPane

  • Tabs: 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 inherited

  • java.awt.Container
  • javax.swing.JComponent
  • javax.swing.JTabbedPane
  • javax.swing.JContainer

Programs to implement JTabbedPane

Example 1: JTabbedPane with Labels

Below 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:

Simple JTabbedPane with three tabs, each containing a label

Example 2: Dynamically add and remove tabs from a JTabbedPane

Below 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:

Add and Remove Tabs Dynamically



Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads