- 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 add new tabs to JTabbedPane from a JMenu in Java?
JTabbedPane
- A JTabbedPane is a component can extend JComponent class and it can provide easy access to more than one panel.
- 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.
- The important methods of JTabbedPane are add(), addTab(), fireStateChanged(), createChangeListener(), setSelectedIndex(), getTabCount() and etc.
Example
import java.awt.*; import java.awt.event.*; import javax.swing.*; import javax.swing.event.*; public class JTabbedPaneTest extends JFrame implements ActionListener { JTabbedPane tabbedPane; int ntabs = 0; public JTabbedPaneTest() { getContentPane().setLayout(new BorderLayout()); tabbedPane = new JTabbedPane(); createTab(); getContentPane().add(BorderLayout.CENTER, tabbedPane); setJMenuBar(createMenuBar()); tabbedPane.addChangeListener(new ChangeListener() { public void stateChanged(ChangeEvent ce) { System.out.println("Tab " + (tabbedPane.getSelectedIndex() + 1) + " is selected"); } }); setTitle("JTabbedPane Test"); setLocationRelativeTo(null); setSize(new Dimension(350, 275)); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); setVisible(true); } protected JMenuBar createMenuBar() { JMenuBar menuBar = new JMenuBar(); JMenu menu = new JMenu("JTabbedPane"); JMenuItem menuItem = new JMenuItem("Create new tab"); menuItem.addActionListener(this); menu.add(menuItem); menuBar.add(menu); return menuBar; } public void actionPerformed(ActionEvent e) { if (e.getActionCommand().equals("Create new tab")) { createTab(); } } protected void createTab() { ntabs++; tabbedPane.addTab("Tab #" + ntabs, new JLabel("Tab #" + ntabs)); } public static void main(String []args) { new JTabbedPaneTest(); } }
Output
- Related Articles
- How can we insert multiple tabs into a single JTabbedPane in Java?
- How can we change the default font of the JTabbedPane tabs in Java?
- How to enable Scrolling Tabs in a JTabbedPane Container
- How to add a Tab in JTabbedPane with Java?
- How can we highlight the selected tab of a JTabbedPane in Java?
- How to set the location of the Tabs in a JTabbedPane Container to the left in Java?
- Display all the titles of JTabbedPane tabs on Console in Java
- How can we add padding to a JTextField in Java?
- How can we add a JSONArray to JSONObject in Java?
- How to set TabLayout Policy to JTabbedPane in Java when all the tabs does not fit in a single run
- How can we add a JSONArray within JSONObject in Java?
- How can we add/insert a JButton to JTable cell in Java?
- Can we display a JTabPane with TextArea in one of the tabs with Java
- How to customize how a JTabbedPane looks in Java?
- Can we add null elements to a Set in Java?

Advertisements