-2

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

Balex
  • 1
  • My guess is that you are messing up the instances buttonX in the actionListener since you are creating a new ActionListener each time, when you do `addActionListener.add(new MyClass())`, instead create 1 action listener and add the same to all. For example `addActionListener(this)` – DSquare May 30 '14 at 22:30
  • 1
    You're leaving out key code that would help us diagnose your problem, so to solve this, you'll have to improve your question, or we'll have to engage in a little voodoo, but one perhaps related concern I have is that your description and code snippets suggests that you have program logic present inside of your paintComponent method, and if so, this should not be. – Hovercraft Full Of Eels May 30 '14 at 22:30
  • Bottom line -- try to avoid making us guess or do voodoo. Please make your question and your code more complete. Create and post a [minimal example program](http://stackoverflow.com/help/mcve). – Hovercraft Full Of Eels May 30 '14 at 22:31
  • Also -- avoid having your GUI class implement your listener as that's asking the class to do too much. Instead use either anonymous inner class or stand along listener classes. – Hovercraft Full Of Eels May 30 '14 at 22:32
  • hi guys, i have tried to create an minimal program to show this problem. But the problem was, that this example worked. I am completely confused... i will add some code to my question, if i can rebuild this problem. But thank you guys for your answers. – Balex May 31 '14 at 23:57

2 Answers2

2

You should not have GUI classes implement listener interfaces as it leads to classes with high coupling and low cohesion. Instead consider using anonymous inner classes for your simple one-off listener code. For instance:

button4.addActionListener(new ActionListener() {
  public void actionPerformed(ActionEvent e) {
    panel1.repaint();
  }
});

Having said this, I worry if your ActionListener is this simple, and suggests to me that you've got some class state-changing going on in panel1's paintComponent(Graphics g) method. Please understand that you do not have full control over when or even if paintComponent will be called, and also you don't ever want to hinder this method with non-painting code, and so you will want to keep all class state-changing code out of this method.

Hovercraft Full Of Eels
  • 283,665
  • 25
  • 256
  • 373
0

I don't understand what's your problem exactly, but try to change

buttonX.addActionListener( new MyClass() );

to

buttonX.addActionListener(this);
BilalDja
  • 1,072
  • 1
  • 9
  • 17