title: using java swing Timer to process and append text into textarea
body: <p>I have a JTextArea txtProcess and 2 methods to process:</p>
  
  <p>First method is to process multiplying numbers less than 10 using for loop. After multiplying them, all results will be appended to txtProcess using Timer. </p>
  
  <p>I use timer to delay the appending. For example, first result appended to txtProcess. Then 500 miliseconds later, second result appended to txtProcess. And so forth until all results appended to txtProcess.</p>
  
  <p>Like this:</p>
  
  <pre><code>int a = 10;
  int result = 0;
  for(int i=1; i &lt;= a; i++){
      result = i * a;
      txtProcess.append("Result "+ i +" = "+ result);
  }
  </code></pre>
  
  <p>Below is piece of code I've tried for first method.</p>
  
  <pre><code>    void first(){
          ActionListener listen = new ActionListener(){
              public void actionPerformed(ActionEvent e){
                  for(int i=1; i &lt;= a; i++){
                      result = i * a;
                      txtProcess.append("Result "+ i +" = "+ result +"\n");
                      if(i == 9){
                          ((Timer)e.getSource()).stop();
                      }
                  }
              }
          };    
          Timer mine = new Timer(500, listen);
          mine.start();
      }
  </code></pre>
  
  <p>But, it doesn't work as I expected. I expected the results append to txtProcess one by one, not simultaneously. This is <strong>the first problem</strong>. How can I fix this?</p>
  
  <p>When all process in first method already executed, the process continue to second method.</p>
  
  <p>There is time interval between process of first method to second method.</p>
  
  <p>I mean like this: after first method execution is finished, the second method execution will be started 2 seconds later. As you see, the time interval is 2 seconds (or probably longer).</p>
  
  <p>So, I tried like this:</p>
  
  <pre><code>    void second(){
          ActionListener listen = new ActionListener(){
              public void actionPerformed(ActionEvent e){
                  for(int i=1; i &lt;= a; i++){
                      result = i * a;
                      txtProcess.append("Result "+ i +" = "+ result +"\n");
                      if(i == 9){
                          ((Timer)e.getSource()).stop();
                      }
                  }
              }
          };    
          Timer mine = new Timer(500, listen);
          mine.start();
      }
  </code></pre>
  
  <p>Then I created another method to combine both of them:</p>
  
  <pre><code>       void combine(){
              ActionListener listen = new ActionListener(){
                  public void actionPerformed(ActionEvent e){
                      first();
                      second();
                  }
              };    
              Timer mine = new Timer(500, listen);
              mine.start();
          }
  </code></pre>
  
  <p>But, both first and second method executed simultaneously. This is <strong>the second problem</strong>: to create interval time between first method and second method. How can I fix this?</p>
  
  <p><strong>Note:</strong>
  You might think this question is duplicated with <a href="https://stackoverflow.com/questions/18332135/java-for-loop-in-gui-textarea">java for-loop in GUI TextArea</a>. I already read and tried the code there, but it still can't fix the problems. </p>