title: run function on JButton press
body: <p>I'm attempting to make a program in java that uses a robot to press a specific key every few seconds. It has a GUI with a start and stop button and a label which tells which state its in. I've got everything working so far except that when I click "start" it runs the loop for my robot function (which is infinite) it doesn't enable the stop button like I thought it would. I know its something stupid with where the infinite loop is placed but I'm not sure how to make it work correctly.</p>
  
  <p>I don't do a lot of java work, this was just a fun thing I thought to try but got stuck part way through. Any help is appreciated. </p>
  
  <pre><code>import java.awt.AWTException;
  import java.awt.FlowLayout;
  import java.awt.Robot;
  import java.awt.event.ActionEvent;
  import java.awt.event.ActionListener;
  import java.awt.event.KeyEvent;
  
  import javax.swing.JButton;
  import javax.swing.JFrame;
  import javax.swing.JLabel;
  
  public class Main extends JFrame {
  
      /**
      * 
      */
      private static final long serialVersionUID = 1L;
      private static boolean running = false;;
      private JButton start_button;
      private JButton stop_button;
      private JLabel tl;
      private static int i = 0;
      Robot robot;
  
      void start() {
  
          JFrame frame = new JFrame("Helper");
          tl = new JLabel("Running: " + running);
          start_button = new JButton("Start");
          stop_button = new JButton("Stop");
          stop_button.setEnabled(false);
          frame.add(tl);
          frame.add(start_button);
          frame.add(stop_button);
          frame.setSize(300, 100);
          frame.setVisible(true);
          frame.setLayout(new FlowLayout());
          frame.setDefaultCloseOperation(EXIT_ON_CLOSE);
          frame.setLocation(400, 400);
  
          try {
              robot = new Robot();
          } catch (AWTException e2) {
              // TODO Auto-generated catch block
              e2.printStackTrace();
          }
          robot.setAutoDelay(200);
  
          start_button.addActionListener(new ActionListener() {
              @Override
              public void actionPerformed(ActionEvent e) {
                  start_button.setEnabled(false);
                  stop_button.setEnabled(true);
                  running = true;
                  tl.setText("Running: " + running);
                  while (running) {
                      robot_loop(robot);
                  }
  
              }
          });
          stop_button.addActionListener(new ActionListener() {
              @Override
              public void actionPerformed(ActionEvent e) {
                  start_button.setEnabled(true);
                  stop_button.setEnabled(false);
                  running = false;
                  tl.setText("Running: " + running);
  
              }
          });
  
      }
  
      public static void main(String[] args) {
          new Main().start();
  
      }
  
      private static void robot_loop(Robot robot) {
  
          robot.keyPress(KeyEvent.VK_NUMPAD0);
          robot.keyRelease(KeyEvent.VK_NUMPAD0);
  
          System.out.println("numpad 0 pressed! - " + i);
          i++;
  
      }
  
  }
  </code></pre>