javaswingjtabbedpane

Java JTabbedPane only see one tab


I was making een tabbedpane but all the tabs are on top of each other or there is only on added. but i can't find a solution.

this is my code:

public Frame(int width, int height) {
    setSize(width, height);

    setTitle("NerdyGadgets backoffice");
    setDefaultCloseOperation(EXIT_ON_CLOSE);

    JLabel label = new JLabel("test the label");
    JTabbedPane tabbedPane = new JTabbedPane();

    tabbedPane.addTab("firstpane", label);
    tabbedPane.addTab("secondpane", label);
    tabbedPane.addTab("test", null, label, "test test test");

    add(tabbedPane);
    setVisible(true);
}

and this is the result that i get: https://gyazo.com/4027807aa305beb227ca4a402d43f067


Solution

  • A component can only belong to a single parent, which means that in your case, it will only be applied to the last tab

    setLayout(new BorderLayout());
    JLabel label = new JLabel("test the label");
    JTabbedPane tabbedPane = new JTabbedPane();
    
    tabbedPane.addTab("firstpane", new JLabel("Tab 1"));
    tabbedPane.addTab("secondpane", new JLabel("Tab 2"));
    tabbedPane.addTab("test", null, label, "test test test");
    
    add(tabbedPane);