title: Using a thread loop to update a JFrame
body: <p>ive done some extensive searching on using threads in a loop and whilst I understand the concept how how seperate threads work, I still cant seem to grasp how to implement it in my simple application.</p>
  
  <p>My application consists of a form with a text box. This textbox needs to be updated once ever iteration of a loop. It starts with the press of a button but the loop should also finish with the press of a stop button. Ive used a boolean value to track if its been pressed.</p>
  
  <p>Here is my form code:</p>
  
  <pre><code>package threadtester;
  
  public class MainForm extends javax.swing.JFrame {
  
      public MainForm() {
          initComponents();
      }
  
      private void RunButtonActionPerformed(java.awt.event.ActionEvent evt) {
         ThreadTester.setRunnable(true);
         ThreadTester example = new ThreadTester(2,this);
         example.run();
      }
  
      private void StopButtonActionPerformed(java.awt.event.ActionEvent evt) {
         ThreadTester.setRunnable(false);
      }
  
  
      public static void main(String args[]) {
  
      java.awt.EventQueue.invokeLater(new Runnable() {
              public void run() {
                  new MainForm().setVisible(true);
              }
          });
      }
  
      public void setTextBox(String myString){
      MainTextbox.setText(myString);
      }
  
  }
  </code></pre>
  
  <p>As you can see I have a button that is pressed. When the button is pressed this executes the code thats in a different class called ThreadTester. Here is the code for that class:</p>
  
  <pre><code>package threadtester;
  
  import java.util.logging.Level;
  import java.util.logging.Logger;
  
  public class ThreadTester implements Runnable
  {
      int thisThread;
      MainForm myMainForm;
      private static boolean runnable;
      // constructor
      public ThreadTester (int number,MainForm mainForm)
      {
          thisThread = number;
          myMainForm = mainForm;   
      }
  
      public void run ()
      {
      for (int i =0;i&lt; 20; i++) {
          if(runnable==false){
             break;
          } 
          System.out.println("I'm in thread " + thisThread + " line " + i);
          myMainForm.setTextBox(i + "counter");
          try {
                 Thread.sleep(1000);
          } catch (InterruptedException ex) {
              Logger.getLogger(ThreadTester.class.getName()).log(Level.SEVERE, null, ex);
          }
      } }
  
      public static void setRunnable(Boolean myValue){
         runnable = myValue;
      }
  
      public static void main(String[] args) {
  
          MainForm.main(args);
      }  
  }
  </code></pre>
  
  <p>as you can see the loop has been created on a seperate thread... but the textbox only updates after the loop has finished. Now as far as im aware in my MainForm I created a seperate thread to run the loop on, so I dont understand why its not running? Any guidence would be much appreciated, ive tried looking at examples on stack exchange but I cant seem to get them to fit into my implemntation.</p>
  
  <p>With the recommendation suggested by Tassos my run method now looks like this:</p>
  
  <pre><code>public void run ()
  {
  for (int i =0;i&lt; 20; i++) {
      if(runnable==false){
          break;
      }
      System.out.println("I'm in thread " + thisThread + " line " + i);
  
      final String var = i + "counter";
          java.awt.EventQueue.invokeLater(new Runnable() {
          public void run() {
                  myMainForm.setTextBox(var);
          }
      });
  
  
      try {
              Thread.sleep(1000);
          } catch (InterruptedException ex) {
              Logger.getLogger(ThreadTester.class.getName()).log(Level.SEVERE, null, ex);
          }
  } }
  </code></pre>