Jude
Jude

Reputation: 451

Trouble creating nested Tabs

I am trying to make a program that has one main Tab,eventually I will add more, and then in the panel that the main Tab displaces there should be 4 Tabs. This is a project I'm doing to try and teach my self java so I just want some insight.

import javax.swing.*;

public class logBook extends JFrame{

    public logBook(){
        this.setSize(300,300);
        this.setVisible(true);
        this.setLocationRelativeTo(null);
        JPanel mainPanel = new JPanel();
        JTabbedPane raidSectionsPane = new JTabbedPane();
        raidSectionsPane.addTab("Deltascape",mainPanel);
       JPanel deltascapePanel = new JPanel();
       JTabbedPane deltascapeSections = new JTabbedPane();
       deltascapeSections.addTab("V1.0",deltascapeSections);
        raidSectionsPane.add(deltascapePanel);
        this.add(raidSectionsPane);




    }
    public static void main(String[] args){
        logBook logger = new logBook();
        logger.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
    }
}

Basically I am a bit lost I am guessing it is on my lack of understanding of Frames/panes/panels. If I understood correctly the Frame is the main container that everything will be inside of, while panels are sections in the frame that will be displaying other parts of your program. I am confused on what panes are.

But I can't get how to make the tabs nested.

Upvotes: 0

Views: 285

Answers (2)

bhavna garg
bhavna garg

Reputation: 270

See this code:

JPanel mainPanel = new JPanel();
JTabbedPane raidSectionsPane = new JTabbedPane();
raidSectionsPane.addTab("Deltascape", mainPanel);
JPanel deltascapePanel = new JPanel();
JTabbedPane deltascapeSections = new JTabbedPane();
deltascapeSections.addTab("V1.0", deltascapePanel);
mainPanel.add(deltascapeSections);
this.add(raidSectionsPane);
}

You need to add deltascapeSections to mainPanel and mainPanel to deltascapeSections.

See this image here

Upvotes: 0

MadProgrammer
MadProgrammer

Reputation: 347334

First of all, I'd spend a bit more time reading through How to Use Tabbed Panes

A JTabbedPane is still just another type of component, so you would add it the same way you would add any other component, using the addTab method

Embedded TabPanes

JTabbedPane outter = new JTabbedPane();
JTabbedPane inner = new JTabbedPane();
inner.addTab("Inner Tab", new JPanel());
outter.addTab("Outter Tab", inner);

JFrame frame = new JFrame("Testing");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.add(outter);
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);

I'd be careful doing this though, as it could be visually confusing for users (IMHO)

Upvotes: 1

Related Questions