- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- 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?
JTabbedPane
- A JTabbedPane is a component can extend JComponent class and we can able to see one tab at a time.
- Each tab is associated with a single component that can be displayed when the tab is selected.
- A JTabbedPane can generate a ChangeListener interface when a tab is selected.
- It is also possible to insert multiple tabs into a single JTabbedPane and the important methods of JTabbedPane are addTab(), fireStateChanged(), getTabPlacement(), setSelectedIndex(), getTabCount() and etc.
Example
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(); } }
Output
- Related Articles
- How can we add new tabs to JTabbedPane from a JMenu in Java?
- How can we change the default font of the JTabbedPane tabs in Java?
- How to set TabLayout Policy to JTabbedPane in Java when all the tabs does not fit in a single run
- How to enable Scrolling Tabs in a JTabbedPane Container
- How can we highlight the selected tab of a JTabbedPane in Java?
- How can we sort multiple columns in a single query?
- How can we insert data into a MySQL table?
- Display all the titles of JTabbedPane tabs on Console in Java
- How to insert multiple document into a MongoDB collection using Java?
- How can we insert a new row into a MySQL table?
- How to set the location of the Tabs in a JTabbedPane Container to the left in Java?
- How to insert multiple rows in a table using a single INSERT command in program?
- Insert multiple rows in a single MySQL query
- How to insert only a single column into a MySQL table with Java?
- Can we insert null values in a Java list?

Advertisements