title: ActionListener and for loop
body: <p>Dear friends of Stackoverflow, </p>
  
  <p>here what I'm trying to do: 
  - I would like to have a simple frame with some buttons and a JTextArea
  - I would like to have a loop that, at every iteration, expects for me to click on a button: when I click on this button a bunch of stuff happens, but I can't get it right: 
  - in one attempt I got the for loop to work but it would not stop, at every turn, it just took the first command and performed all 20 turns without stopping
  - in the current version I click the button and nothing happens
  - I have already researched SOF and a series of other sites, including the Oracle documents but (probably also due to my level of experience), I cannot find an explanation that is clear enough for me to understand</p>
  
  <p>here my code</p>
  
  <pre><code>package game4_prova_forloop;
  
  import java.awt.Color;
  import java.awt.FlowLayout;
  import java.awt.event.ActionEvent;
  import java.awt.event.ActionListener;
  import javax.swing.GroupLayout;
  import javax.swing.JButton;
  import javax.swing.JFrame;
  import javax.swing.JPanel;
  import javax.swing.JScrollPane;
  import javax.swing.JTextArea;
  
  public class GAME4_prova_forloop {
  
      public static void main(String[] args) {
  
          //create frame
          JFrame frame = new JFrame("Action Listener");
          frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
          frame.setSize(600, 600);
          frame.setLayout(new FlowLayout());
          frame.setVisible(true);   
  
          //crete text area panel, 
          JPanel pannelloTextArea = new JPanel(); 
          pannelloTextArea.setBackground(new Color(255, 204, 204));
          pannelloTextArea.setSize(400, 400);
          frame.add(pannelloTextArea); 
          GroupLayout pannelloTextAreaLayout = new GroupLayout(pannelloTextArea); 
  
          //create scrollPane
          JScrollPane scrollTextArea = new JScrollPane(); 
  
          //1) create JTextArea
          JTextArea mostraTesto = new JTextArea(20, 20); 
          mostraTesto.setLineWrap(true);  //make it wrap text 
              pannelloTextArea.add(scrollTextArea); //add it to scroll pane
              pannelloTextArea.revalidate();
                  scrollTextArea.add(mostraTesto); //add the scroll pane to text area 
                  scrollTextArea.setViewportView(mostraTesto); 
  
          //2) create buttons 
          JButton action1 = new JButton("1");
          frame.add(action1);
          JButton action2 = new JButton("2");
          frame.add(action2);
          JButton action3 = new JButton("3");
          frame.add(action3);
  
          //3) pass textArea in the RoundLevelAction class
          RoundLevelAction roundLevelActionObj = new RoundLevelAction(mostraTesto); //first issue: I get an error
          //4) add listener to JButtons
          action1.addActionListener(roundLevelActionObj);
          action2.addActionListener(roundLevelActionObj);
          action3.addActionListener(roundLevelActionObj);          
      }
  
  //THIS IS WHERE I START TO HAVE PROBLEMS: WHEN I CLICK NOTHING HAPPENS, WHEN 
  //I WOULD EXPECT THE FOR LOOP TO GO THROUGH ITERATIONS
      public class RoundLevelAction implements ActionListener {
  
          //add inside the listener the pieces of GUI that you'll use
          private JTextArea mostraTesto;
          private Object action1;
          private Object action2;
          private Object action3;
          private Object action4;
          private Object action5;
          private Object action6;
  
          //create class for JTextArea
          public RoundLevelAction(JTextArea mostraTesto){
              this.mostraTesto = mostraTesto; 
          }    
  
          //and, finally, what I really want to do: a loop that, at each turn, expects me to click on 
          //a button and does an action in response
          public void actionPerformed(ActionEvent e) {
              //now create the loop
              for (int round_counter=1; round_counter&lt;21; round_counter++) {                                       
                  if (e.getSource()==action1){                             
                      mostraTesto.append("\n description action 1 and a bunch of other stuff");
                  }
                  else if (e.getSource()== action2){ 
                      mostraTesto.append("\n description action 2 and a bunch of other stuff");
                  } 
                  else if (e.getSource()== action3){
                      mostraTesto.append("\n description action 3 and a bunch of other stuff");
                  } 
              }
          }
      }
  
  }
  </code></pre>
  
  <p>IMPORTANT: I'm well aware that the code above does not fit Java best practices: it's just a sample code to illustrate what I'm trying to do (the original code is a lot of lines in multiple classes)</p>
  
  <p>I hope you can help me to understand where I'm doing wrong</p>
  
  <p>Many thanks in advance</p>