fabio
fabio

Reputation: 13

JTabbedPane created dynamically

I have a problem when I try to crete dynamically Tabs on a JTabbePane by a for. The problem is that I don't know how to access the contents when an event happens.
I will try to show you part of the code to be easy to understand.

            conteudoT = new JTabbedPane(JTabbedPane.TOP);
    conteudoT.setBounds(5, 19, 477, 232);

    for (int i = 0; i < players; i++) {

        conteudo = new JPanel();
        conteudo.setLayout(null);

        Details = new JPanel();
        Details.setBounds(15, 11, 307, 183);
        Details.setVisible(false);
        Details.setName("Details" + i);
        conteudo.add(Details);

                    btnR = new JButton("r");
                    btnR.addActionListener(this);

        conteudoT.addTab("Jogador " + (i + 1), conteudo);
        Details.setLayout(new GridLayout(2, 1, 0, 0));
                    ... 
                    contentPane.add(conteudoT);

                    }


    @Override
public void actionPerformed(ActionEvent e) {

    for (int i = 0; i <  players; i++) {
        if (e.getSource()==btnR) {
            Details.setVisible(true); 

        }
    }
    }

What happens here is that the painel Details is added on the last Tab to be constructed and I would like to add it on the Tab that the event happend.

Upvotes: 1

Views: 343

Answers (1)

Oliver Watkins
Oliver Watkins

Reputation: 13509

use :

conteudoT.indexOfTab(string)

to get the index. Then use :

conteudoT.getTabComponentAt(int index)

to get the component

Upvotes: 1

Related Questions