Before describing the problem itself, I will explain what I am trying to do.
I'm going to need to use some buttons that will basically do the same thing, except that the actions they're going to apply change from one (JTabbedPane)
to another. So I thought that if I used the same component I would save code, besides being more oriented, I'm not sure if what I tried to do is correct, I accept suggestions about it.
I have two problems:
1st: I add the same component in different tabs, and every time I add it I instantiate them (I thought it would be the right thing to do). What happens is that the actions are not happening correctly as I have defined.
2nd: I have defined that the tabs and buttons inside the tabs should be enabled and disabled (disable) when clicking the top buttons (enable and disable), but it 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);
}
}