dynamic jPanel buttons actionCommand
<p>I would like to ask a question related to dynamic <code>button</code> <code>actionPerformed</code>. I have <code>jFrame</code> with content menu which will reference to dynamic <code>jPanel1</code> by <code>cardLayout</code> and <code>jButton1</code>. Dynamic <code>jPanel1</code> contain <code>saveButton</code>. I attached <code>Test ActionListener</code> to <code>jButton1</code>. The problem that I am facing is I have set <code>actionCommand</code> to <code>jPanel1.saveButton</code> as shown in below code. And when I click <code>jButton1</code> output I am getting <code>2</code> and <code>3</code>. I was expected that <code>2nd</code> and <code>3rd</code> <code>jPanel1.SaveButton</code> also was clicked, but just <code>3rd</code> <code>jPanel1.saveButton</code> was clicked. How do I get that <code>2nd</code> and <code>3rd</code> <code>jPanel1.SaveButton</code> were clicked?</p>

<pre><code>import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;

public class Test implements ActionListener {

    public Test() {
    }

    public void actionPerformed(ActionEvent e) {

        for (int i = 0; i&lt;2; i++){
           jPanel1.save.setActionCommand(String.valueOf(i+2));
           String n = jPanel1.save.getActionCommand();
           jPanel1.save.doClick();
           System.out.println("jPanel1: " + n);
        }

           System.out.println("The action have been performed");
    }

    public static void main(String[] agrs) {
           JButton but = new JButton();
           but.addActionListener(new Test());
           but.doClick();
    }  
}

Output:
jPanel1: 2
jPanel1: 3
</code></pre>