Browse DevX
Sign up for e-mail newsletters from DevX

Tip of the Day
Language: User Interface Design
Expertise: Beginner
Feb 5, 1999



Building the Right Environment to Support AI, Machine Learning and Deep Learning

Put a JTabbedPane on a Tab of a JTabbedPane

I want to put a JTabbedPane on a tab of another JTabbedPane for using it as a chapter with different sections below it. How can I do this?

You can add a JTabbedPane to a tab of another JTabbedPane in the same way you would add any other component to a JTabbedPane. JTabbedPane offers several add and addTab methods that allow different levels of flexibility when adding components. Some of the methods allow you to specify specific layout constraints or add components to a specific tab. The easiest way to get started with JTabbedPane is by using add(String title, Component component). This method will add a new tab with the specified title and containing the indicated component. To add a JTabbedPane instance, just use it as the component parameter. User interface design is not my forte, but I have found it easier to navigate documentation by using a tree control on the side, allowing me to view the entire outline. Embedding a JTabbedPane inside of another JTabbedPane may not be the most effective approach. Nonetheless, this example demonstrates how to do it:

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

public class TabbedPaneDemo extends JFrame {
  JTabbedPane _parentTab, _childTab;

  public TabbedPaneDemo() {
    _parentTab = new JTabbedPane();

    _childTab = new JTabbedPane();
    _childTab.add("Section 1", new JLabel("in a galaxy"));
    _childTab.add("Section 2", new JLabel("far, far away ..."));
    _parentTab.add("Chapter 1", new JLabel("A long time ago"));
    _parentTab.add("Chapter 2", _childTab);

    getContentPane().add(_parentTab, BorderLayout.CENTER);

  public static void main(String[] args) {
    Frame frame;
    WindowListener exitListener;

    exitListener = new WindowAdapter() {
      public void windowClosing(WindowEvent e) {
        Window window = e.getWindow();

    frame = new TabbedPaneDemo();
    frame.setSize(400, 400);
DevX Pro
Comment and Contribute






(Maximum characters: 1200). You have 1200 characters left.



Thanks for your registration, follow us on our social networks to keep up-to-date