Ultialize same component in Jtabbedpane

Asked

Viewed 86 times

0

Before describing the problem itself, let me explain what I’m trying to do.

I will need to use some buttons that will do basically the same thing, only the actions they will apply change from a tab (JTabbedPane) to another. So, I thought that if I used the same component I would save code in addition to being more oriented, I’m not sure if what I tried to do is correct, I accept suggestions regarding this.

I have two problems:

1º: I add the same component in different tabs, and every time I add it I urge them (I thought it would be the right thing to do). What happens is that the actions are not happening correctly as I defined.

2º: I defined that the tabs and buttons inside the tabs should be enabled (enable) and disabled (disable) when clicking the top buttons (enable and disable), but he is not doing this for everyone.

import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JComponent;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTabbedPane;

public class NewClass extends JFrame implements ActionListener {

    private final JTabbedPane tabbedPane = new JTabbedPane();
    private JPanel painelBotao = new JPanel();
    private JButton habilita = new JButton("Habilitar");
    private JButton desabilita = new JButton("Desabilitar");
    private JButton botao01 = new JButton();
    private JButton botao02 = new JButton();

    public static void main(String[] args) {
        NewClass n = new NewClass();
        n.setVisible(true);
    }

    public NewClass() {
        JPanel jp = new JPanel();

        jp.add(habilita);
        jp.add(desabilita);
        botaoHabilita();
        botaoDesabilita();

        tabbedPane.addTab("Aba 01", aba01());
        tabbedPane.addTab("Aba 02", aba02());
        tabbedPane.setPreferredSize(new Dimension(400, 200));
        add(tabbedPane, BorderLayout.BEFORE_LINE_BEGINS);
        jp.add(tabbedPane);
        add(jp);
        botaoAba01();
        botaoAba02();
        habilita(false);
        setSize(500, 320);
        setLocationRelativeTo(null);
        setDefaultCloseOperation(EXIT_ON_CLOSE);
    }

    public JComponent aba01() {
        JPanel jp = new JPanel();

        adicionaBotaoItem(botao01 = new JButton("Botão 1"));
        jp.add(painelBotao = new JPanel());

        adicionaBotaoItem(botao02 = new JButton("Botão 2"));
        jp.add(painelBotao = new JPanel());
        return jp;
    }

    public JComponent aba02() {
        JPanel jp = new JPanel();

        adicionaBotaoItem(botao01 = new JButton("Botão 1"));
        jp.add(painelBotao = new JPanel());

        adicionaBotaoItem(botao02 = new JButton("Botão 2"));
        jp.add(painelBotao = new JPanel());
        return jp;
    }

    private void adicionaBotaoItem(JButton botao) {
        painelBotao.add(botao);
        botao.addActionListener(this);
        painelBotao.setLayout(new GridLayout(1, 2));
    }

    @Override
    public void actionPerformed(ActionEvent e) {
    }

    public void botaoAba01() {
        botao01.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent ae) {
                JOptionPane.showMessageDialog(null, "Botão 01 - aba 01");
            }
        });

        botao02.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent ae) {
                JOptionPane.showMessageDialog(null, "Botão 02 - aba 01");
            }
        });
    }

    public void botaoAba02() {
        botao01.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent ae) {
                JOptionPane.showMessageDialog(null, "Botão 01 - aba 02");
            }
        });

        botao02.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent ae) {
                JOptionPane.showMessageDialog(null, "Botão 02 - aba 02");
            }
        });
    }

    public void botaoHabilita() {
        habilita.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent ae) {
                habilita(true);
            }
        });
    }

    public void botaoDesabilita() {
        habilita.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent ae) {
                habilita(false);
            }
        });
    }

    public void habilita(boolean status) {
        tabbedPane.setEnabled(status);
        botao01.setEnabled(status);
        botao02.setEnabled(status);
    }
}
  • Let me get this straight, you want to "recycle" buttons between tabs, but in each tab they will have different actions, is THIS?

  • @diegofm can - if you say so, but I’m not sure if I should do it, I just started doing it like this because I thought I was repeating code too much

  • What is the function of these buttons? It’s a bit confusing the code. What do you want with each one of them?

  • @diegofm ai only made a simple example, but in real they will include and exclude rows from a table, each tab has a different table. I should create different buttons for each tab ?

  • 1

    So it is not a good to reuse buttons in this case.

  • ok, it was a question whether to do it or not. In case, I delete the post ? or edit it ?

  • 1

    No, I think you can suggest something. I will try to answer with an idea I had. I just don’t know if it will fit with this new table information. It would be interesting to edit the question with this situation of the tables too, then already kills the problem once and for all.

Show 2 more comments
No answers

Browser other questions tagged

You are not signed in. Login or sign up in order to post.