I have a problem by programming an user interface. I am using Swing.
I begin to create a Tabpane with 5 Tabs.
Tab 1 contains: Button1 and a textarea
Tab 2 contains: Button2 and a textarea
Tab 3 contains: Button3 and a textarea
Tab 4 contains: Button4 and Panel1
Tab 5 contains: Button5 and Panel2
This class MyClass implements the interface ActionListener and the method actionPerformed was overwritten like this (short version):
public class MyClass implements ActionListener {
// Creating UI ... and so on
public void actionPerformed(ActionEvent e) {
if (e.getSource() == button1) {
//do something
}
// Exactly the same for button2 and button3
if (e.getSource() == button4) {
panel1.repaint(); // PANEL 1
}
if (e.getSource() == button5) {
panel2.repaint(); // PANEL 2
}
}
}
I have two classes Panel1 and Panel2, which extend the class JPanel.
public class Panel1 extends JPanel {
protected Panel1() {
}
@Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
//DRAW SOMETHING ON PANEL1
}
}
The class for Panel2 looks exactly like this class, but draws something on panel2.
My problem is now:
In MyClass i have created the UI and all components for it. This includes 5 buttons. On each of these 5 buttons i have called the method: buttonX.addActionListener( new MyClass() );
to do exactly what is defined in the actionPerformed() - method.
If i add this ActionListener to button1-3 : Everything works fine.
If i add this ActionListener to button1-4 (NOT 5) : button1-3 do what they should do and button4 repaints Panel1 correctly.
If i add this ActionListener to button1-3 and 5 (NOT 4) : button1-3 do what they should do and button5 repaints Panel2 correctly.
BUT if i add this ActionListener to button1-5 : button1-3 do what they should do and button5 repaints Panel2 correctly. BUT button4 does nothing!. So Panel1 was not repainted!
Can somebody explain me, why button1-3 always work; button4 and button5 work, if i add the ActionListener to just ONE of theses two buttons; BUT button4 does NOT work, if i add the ActionListener to all 5 buttons?!
Thanks guys for your help
Best regards
Alex