Dynamically adding tabs to JTabbedPane

时间:2016-04-25 09:25:04

标签: java swing jtabbedpane

In my main I loop over each League as below:

for (League l : t.getLeagues()) {
    LeaguePanel leaguePanel = new LeaguePanel(l);
    roundTabs.addTab(l.getName(), leaguePanel);
}

This should then create a JPanel and add it to the tab.

public class LeaguePanel extends JPanel {

    private League league;
    private JComboBox roundComboBox;

    LeaguePanel(League l) {
        league = l;
        JPanel leagePanel = new JPanel();
        leagePanel.add(new JLabel("Tournament Information"));
    }

However, the tab gets created but nothing appears within it

Any ideas why?

1 个答案:

答案 0 :(得分:4)

You want to add things to your LeaguePanel object, not to a new JPanel :

JPanel leagePanel = new JPanel();
leagePanel.add(new JLabel("Tournament Information"));

becomes

this.add(new JLabel("Tournament Information"));

Because it's your LeaguePanel that you add to the tabbed pane .