title: Stopping and starting a loop with a button
body: <p>I am trying to control a while loop in my program to stop and start based on a user input. I have attempted this with a button and the "start" part of it works but then the code goes into an infinite loop which I cant stop without manually terminating it. The following is all my code:
  <strong>Header Class</strong></p>
  
  <pre><code>package test;
  
      import javax.swing.JFrame;
  
      public class headerClass {
          public static void main (String[] args){
              frameClass frame = new frameClass();
              frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
              frame.setSize(150,75);
              frame.setVisible(true);
          }
      }
  </code></pre>
  
  <p><strong>Frame Class</strong></p>
  
  <pre><code>package test;
  
  import javax.swing.*;
  import java.awt.*;
  import java.awt.event.*;
  
  public class frameClass extends JFrame {
  
      private JButton click;
  
      public frameClass(){
          setLayout(new FlowLayout());
          click = new JButton("Stop Loop");
          add(click);
  
          thehandler handler = new thehandler();
          click.addActionListener(handler);
      }
  
      private class thehandler implements ActionListener{
          public void actionPerformed(ActionEvent e){
              if(e.getSource()==click){
                  looper loop = new looper();
                  looper.buttonSet = !looper.buttonSet;
              }
          }
      }
  }
  </code></pre>
  
  <p><strong>Looping class</strong></p>
  
  <pre><code>package test;
  
  public class looper {
      public static boolean buttonSet;
  
      public looper(){
          while (buttonSet==false){
              System.out.println("aaa");
          }       
      }
  }
  </code></pre>
  
  <p>How do I fix this and stop if from going into the infinite loop please?</p>