Java JTabbedPane is a component in the Java Swing library that allows users to create a tabbed interface in their graphical user interface (GUI) applications. With JTabbedPane, users can display multiple components or panels in a single window and switch between them by clicking on the corresponding tabs.
To use JTabbedPane, you can start by creating a new instance of the class:
JTabbedPane tabbedPane = new JTabbedPane();
Then, you can add components or panels to the JTabbedPane using the addTab()
method, which takes two parameters: the title of the tab and the component or panel to be displayed:
JPanel panel1 = new JPanel(); tabbedPane.addTab("Tab 1", panel1); JPanel panel2 = new JPanel(); tabbedPane.addTab("Tab 2", panel2);
You can also customize the appearance and behavior of the JTabbedPane using various methods and properties, such as setting the tab placement, changing the colors and fonts of the tabs, and adding icons or tooltips to the tabs.
Once you have added all the components or panels to the JTabbedPane, you can add it to your GUI container, such as a JFrame or JDialog:
JFrame frame = new JFrame(); frame.getContentPane().add(tabbedPane); frame.pack(); frame.setVisible(true);
This will display the JTabbedPane in your GUI application, allowing users to switch between the tabs and view the contents of each panel.
JTabbedPane class declaration:
The JTabbedPane
class is part of the Java Swing library and is used to create a tabbed pane component. A tabbed pane consists of a group of tabs, where each tab represents a different component that can be displayed within the same area of a user interface. The user can switch between tabs to view the different components.
Here is the basic declaration of the JTabbedPane
class:
public class JTabbedPane extends JComponent implements Serializable, Accessible, ChangeListener, SingleSelectionModel
This class extends the JComponent
class and implements several interfaces, including Serializable
, Accessible
, ChangeListener
, and SingleSelectionModel
.
The Serializable
interface is used to indicate that objects of this class can be serialized (i.e., converted to a stream of bytes that can be saved to a file or transmitted over a network).
The Accessible
interface is used to provide accessibility information for users who have disabilities that affect their ability to interact with the user interface.
The ChangeListener
interface is used to listen for changes in the selected tab, while the SingleSelectionModel
interface is used to represent the selection state of the tabbed pane.
Commonly used Constructors:
Here are the commonly used constructors of the JTabbedPane
class:
JTabbedPane()
– Creates a newJTabbedPane
instance with no tabs and a default tab placement (which isTOP
).JTabbedPane(int tabPlacement)
– Creates a newJTabbedPane
instance with no tabs and the specified tab placement, which can be one ofTOP
,BOTTOM
,LEFT
, orRIGHT
.JTabbedPane(int tabPlacement, int tabLayoutPolicy)
– Creates a newJTabbedPane
instance with no tabs and the specified tab placement and tab layout policy. The tab layout policy can be one ofWRAP_TAB_LAYOUT
orSCROLL_TAB_LAYOUT
.JTabbedPane(int tabPlacement, int tabLayoutPolicy, int selectedIndex)
– Creates a newJTabbedPane
instance with no tabs and the specified tab placement, tab layout policy, and selected tab index.JTabbedPane(int tabPlacement, int tabLayoutPolicy, int selectedIndex, Color tabAreaBackground)
– Creates a newJTabbedPane
instance with no tabs and the specified tab placement, tab layout policy, selected tab index, and tab area background color.
Note that in all these constructors, the tabPlacement
parameter specifies where the tabs should be placed in the tabbed pane (top, bottom, left, or right), while the tabLayoutPolicy
parameter specifies how the tabs should be laid out when there are too many to fit in the available space (wrap or scroll). The selectedIndex
parameter specifies which tab should be initially selected, and the tabAreaBackground
parameter specifies the background color of the tab area:
Java JTabbedPane Example:
Here is an example of how to create and use a JTabbedPane
in Java:
import javax.swing.*; import java.awt.*; public class MyTabbedPaneExample extends JFrame { public MyTabbedPaneExample() { super("My Tabbed Pane Example"); JTabbedPane tabbedPane = new JTabbedPane(); JPanel panel1 = new JPanel(); JPanel panel2 = new JPanel(); JPanel panel3 = new JPanel(); panel1.add(new JLabel("This is Panel 1")); panel2.add(new JLabel("This is Panel 2")); panel3.add(new JLabel("This is Panel 3")); tabbedPane.addTab("Tab 1", panel1); tabbedPane.addTab("Tab 2", panel2); tabbedPane.addTab("Tab 3", panel3); getContentPane().add(tabbedPane, BorderLayout.CENTER); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); setSize(300, 200); setVisible(true); } public static void main(String[] args) { new MyTabbedPaneExample(); } }
This example creates a JTabbedPane
with three tabs (Tab 1
, Tab 2
, and Tab 3
), each containing a JPanel
with a JLabel
. The JTabbedPane
is added to the JFrame
using the getContentPane().add()
method, and the JFrame
is displayed on the screen using the setVisible(true)
method. When you run the program, you should see a window with three tabs and a label in each tab.