title: How do I create another thread so that my ActionListener still listens for button press events while my planet simulation runs?
body: <p>I have a JButton and have added an <code>ActionListener</code> to it. I also have a <code>SimulationPanel</code> class which extends <code>JPanel</code> and have an instance of it named <strong>simulationPanel</strong>. Both are added to a <code>JFrame</code>. Within in the <strong>actionPerformed</strong> method of the <code>ActionListener</code> I call <code>simulationPanel.startSimulation()</code>.</p>
  
  <pre><code>class StartButtonListener implements ActionListener{
      @Override
      public void actionPerformed(ActionEvent arg0) {
          if(mode == MODE_DEFAULT){
              mode = MODE_SIMULATING;
              startButton.setText("Stop Simulation");
              simulationPanel.startSimulation();                              
          } else if(mode == MODE_SIMULATING){
              mode = MODE_DEFAULT;
              startButton.setText("Start Simulation");
              simulationPanel.stopSimulation();
          }
      }
  }
  </code></pre>
  
  <p>The <code>stopSimulation()</code> method sets running to false.</p>
  
  <pre><code>public void startSimulation() {
      running = true;
      long time = System.currentTimeMillis();
      long time2 = System.currentTimeMillis();
      while(running){
          time = System.currentTimeMillis();
          if(time - time2 &gt; 1000/60){
              for(int i = 0; i &lt; planets.size(); i++) {
                  planets.get(i).setLocation(planets.get(i).getNewLocation());
              }
              this.paintImmediately(0, 0, 950, 680);
              time2 = System.currentTimeMillis();
          }
      }
  }
  </code></pre>
  
  <p>This sets the new location of my planets and repaints the screen every 60th of a second. </p>
  
  <p>The problem is that once it has entered the while(running) loop the <code>ActionListener</code> no longer responds to the button being pressed and therefore <code>stopSimulation()</code> can't be called. How would I go about creating a new thread that makes the button able to respond while simulation is running? </p>
  
  <p><a href="https://gist.github.com/anonymous/488eaa8bbed6a442c39e" rel="nofollow">The entire program</a></p>