
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- 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 insert multiple tabs into a single JTabbedPane in Java?
In this article, we will learn to insert multiple tabs into a single JTabbedPane in Java. The Java Swing's JTabbedPane enables you to group content into several tabs in one container.
JTabbedPane
A JTabbedPane is a component can extends the JComponent class, and we can able to see only one tab at a time. Each tab is associated with a single component that can be displayed when the tab is selected.
Syntax
Object declaration and instantiation using the Java class JTabbedPane:
JTabbedPane tabbedPane = new JTabbedPane();
A JTabbedPane can generate a ChangeListener interface when a tab is selected. It is also possible to insert multiple tabs into a single JTabbedPane.
Methods
The important methods of JTabbedPane are:
- addTab()
- fireStateChanged()
- getTabPlacement()
- setSelectedIndex()
- getTabCount()
Inserting multiple tabs into a single JTabbedPane
In the example, we are using the Swing package's JTabbedPane class for managing several tabs in one window. Each tab may contain a different component, like a panel, a label, or a text field.
The following is the step-by-step process to insert multiple tabs into a single JTabbedPane in Java:
Constructor
The constructor constructs the JFrame window with the header "Nested JTabbedPane test".
public NestedJTabbedPaneTest() { setTitle("Nested JTabbedPane test"); setLayout(new BorderLayout());
JTabbedPane Setup
Creates the JTabbedPane named "tabbedPane" and adds it to the center of the JFrame using the borderLayout. Calls createNestedTab() to start creating the nested tabs.
JTabbedPane tabbedPane = new JTabbedPane(); add(BorderLayout.CENTER, tabbedPane); createNestedTab(tabbedPane, 1);
createNestedTab() Method
The createNestedTab() function recursively constructs nested JTabbedPane components to a depth of 5 levels, with each new tab serving as a parent for the next nested pane, thus forming a hierarchical tabbed structure.
protected void createNestedTab(JTabbedPane jtp, int count) { if (count > 5) return; JTabbedPane tp = new JTabbedPane(); tp.setTabPlacement(getNextOrientation(jtp.getTabPlacement())); jtp.addTab("Tab #" + count, tp); createNestedTab(tp, count+1); }
Example
Below is an example for inserting multiple tabs into a single JTabbedPane in Java:
import java.awt.*; import java.awt.event.*; import javax.swing.*; import javax.swing.event.*; public class NestedJTabbedPaneTest extends JFrame { public NestedJTabbedPaneTest() { setTitle("Nested JTabbedPane test"); setLayout(new BorderLayout()); JTabbedPane tabbedPane = new JTabbedPane(); add(BorderLayout.CENTER, tabbedPane); createNestedTab(tabbedPane, 1); setSize(375, 250); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); setLoc*ationRelativeTo(null); setVisible(true); } protected void createNestedTab(JTabbedPane jtp, int count) { if (count > 5) return; JTabbedPane tp = new JTabbedPane(); tp.setTabPlacement(getNextOrientation(jtp.getTabPlacement())); jtp.addTab("Tab #" + count, tp); createNestedTab(tp, count+1); } private int getNextOrientation(int tabPlacement) { if (tabPlacement == JTabbedPane.TOP) return JTabbedPane.RIGHT; if (tabPlacement == JTabbedPane.RIGHT) return JTabbedPane.BOTTOM; if (tabPlacement == JTabbedPane.BOTTOM) return JTabbedPane.LEFT; else return JTabbedPane.TOP; } public static void main(String []args) { new NestedJTabbedPaneTest(); } }