VOOZH about

URL: https://www.geeksforgeeks.org/java/java-jtabbedpane/

⇱ Java JTabbedPane - GeeksforGeeks


  • Courses
  • Tutorials
  • Interview Prep

Java JTabbedPane

Last Updated : 28 Apr, 2025

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:

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:

Comment