title: How to stop thread / timer inside that thread?
body: <p>I have timer inside a run() method created by windowbuilder in eclipse. </p>
  
  <p>I do not know how to stop that timer from another method. For example by adding </p>
  
  <pre><code>if(MenuWindow.gameStarted == false) gameTimer.stop();
  </code></pre>
  
  <p>that is here, but when I change it to false it does not change. It changes to false and returns to true (I think).</p>
  
  <p>I am changing that variable from class <code>MenuWindow</code></p>
  
  <pre><code>package krystofee.niggatycoon;
  
  public class MenuWindow {
  
  private static JFrame frame;
  static boolean gameStarted;
  static GameWindow game;
  
  /**
   * Launch the application.
   */
  public static void main(String[] args) {
      EventQueue.invokeLater(new Runnable() {
          public void run() {
              try {
                  MenuWindow window = new MenuWindow();
                  window.frame.setVisible(true);
              } catch (Exception e) {
                  e.printStackTrace();
              }
          }
      });
  }
  
  /**
   * Create the application.
   */
  public MenuWindow() {
      initialize();
  }
  
  /**
   * Initialize the contents of the frame.
   */
  private void initialize() {
      frame = new JFrame();
      frame.setBounds(100, 100, 450, 300);
      frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
      frame.getContentPane().setLayout(null);
  
      JButton btnNewButton = new JButton("Start");
      btnNewButton.addActionListener(new ActionListener() {
  
          public void actionPerformed(ActionEvent arg0) {
              GameWindow game = new GameWindow();
              game.StartGame();
              gameStarted = true;
              frame.setVisible(false);
          }
      });
      btnNewButton.setBounds(144, 154, 146, 43);
      frame.getContentPane().add(btnNewButton);
  }
  
  public static void open() {
      frame.setVisible(true); 
      game = null;
      gameStarted = false;
  }
  }
  </code></pre>
  
  <p>And there is <code>GameWindow</code></p>
  
  <pre><code>public class GameWindow&lt;E&gt; {
  
  private JFrame frame;
  static JProgressBar progressBar1;
  static JButton upGardenerButton;
  private Timer gameTimer;
  
  public static Player player;
  public static Worker worker;
  private static JLabel labelMoney;
  private JTable table;
  private JLabel lblMultiplier;
  boolean timerStop;
  
  /**
   * Create the application.
   */
  public GameWindow() {
      worker = new Worker(50, 75, 5000);
      player = new Player();
  
      initialize();
  }
  
  protected void endGame() {
      frame.dispose();
      timerStop = true;
  }
  
  public void StartGame() {
      EventQueue.invokeLater(new Runnable() {
  
          int gardenerTime = 0;
  
          public void run() {
              try {
                  GameWindow&lt;Object&gt; window = new GameWindow&lt;Object&gt;();
                  window.frame.setVisible(true);
                  ActionListener taskPerformer = new ActionListener() {
  
                      @Override
                      public void actionPerformed(ActionEvent arg0) {
                          gardenerTime += 10;
                          progressBar1.setValue(gardenerTime);        
                          if(gardenerTime &gt;= worker.time) {
                              player.money.add(worker.getProfit());
                              gardenerTime = 0;
                          }
  
                          labelMoney.setText(player.money.getMoney()+"$");    
  
                          if(gardenerTime % 100 == 0) System.out.println("Timer bezi. " + MenuWindow.gameStarted );
  
                          if(MenuWindow.gameStarted == false) gameTimer.stop();
                      }
                  };
  
                  int delay = 10;
                  gameTimer = new Timer(delay, taskPerformer);
                  gameTimer.start();
  
              } catch (Exception e) {
                  e.printStackTrace();
              }
          }
      });
  }
  
  /**
   * Initialize the contents of the frame.
   */
  private void initialize() {
      frame = new JFrame();
      frame.getContentPane().setBackground(UIManager.getColor("Panel.background"));
      frame.setBounds(100, 100, 571, 287);
  
      WindowListener exitListener = new WindowAdapter() {
  
          @Override
          public void windowClosing(WindowEvent e) {
              int confirm = JOptionPane.showOptionDialog(
                   null, "Opravdu chceš ukončit hru?", 
                   "Konec?!", JOptionPane.YES_NO_OPTION, 
                   JOptionPane.QUESTION_MESSAGE, null, null, null);
              if (confirm == 0) {
                 frame.setVisible(false);
                 endGame();
                 System.out.println("timerstop: "+timerStop);
                 //MenuWindow.open();
              }
          }
      };
  
      frame.addWindowListener(exitListener);
      frame.getContentPane().setLayout(null);
  
      progressBar1 = new JProgressBar();
      progressBar1.setBounds(6, 89, 146, 27);
      progressBar1.setStringPainted(true);
      progressBar1.setMaximum(worker.time);
      progressBar1.setMinimum(0);
      progressBar1.setString(worker.getProfit()+"$");
      frame.getContentPane().add(progressBar1);
  
      upGardenerButton = new JButton(worker.getUpgradeCost()+" $   "+"level "+worker.level);
      upGardenerButton.setBounds(159, 89, 165, 27);
      upGardenerButton.addActionListener(new ActionListener() {
          public void actionPerformed(ActionEvent arg0) {
              if(player.money.getMoney() &gt;= worker.getUpgradeCost()) {
                  worker.upgrade();
              }
          }
      });
      frame.getContentPane().add(upGardenerButton);
  
      JPanel panel = new JPanel();
      panel.setBounds(6, 37, 175, 32);
      panel.setBorder(new BevelBorder(BevelBorder.LOWERED, null, null, null, null));
      panel.setBackground(Color.LIGHT_GRAY);
      frame.getContentPane().add(panel);
      panel.setLayout(new MigLayout("", "[149px]", "[23px]"));
  
      labelMoney = new JLabel("");
      labelMoney.setHorizontalTextPosition(javax.swing.SwingConstants.RIGHT);
      panel.add(labelMoney, "cell 0 0,alignx trailing,aligny baseline");
  
      JTabbedPane tabbedPane = new JTabbedPane(JTabbedPane.TOP);
      tabbedPane.setBounds(357, 6, 192, 236);
      frame.getContentPane().add(tabbedPane);
  
      JScrollPane scrollPane = new JScrollPane();
      tabbedPane.addTab("Upgrades", null, scrollPane, null);
  
      String[] columnNames = {"Upgrades", ""};
      Object[][] data =
      {
          {"Vozík +2",    "2000$"},
          {"Kalhoty +3",  "15000$"},
          {"Šperháky +4", "50000$"},
          {"Auto *2",     "200000$"},
      };
  
      DefaultTableModel model = new DefaultTableModel(data, columnNames){
           @Override
           public boolean isCellEditable(int row, int column) {
                 if(column != 1) return false;
                 else return true;
              }
      };
      JTable table = new JTable( model );
  
      Action upgradeMultiplier = new AbstractAction()
      {
          int count = 0;
          public void actionPerformed(ActionEvent e)
          {
              JTable table = (JTable)e.getSource();
              int modelRow = Integer.valueOf( e.getActionCommand() );                
              String sPrice = (String) table.getModel().getValueAt(modelRow, 1);
              int price = Integer.parseInt(sPrice.substring(0,sPrice.length()-1));    
              String sMultip = (String) table.getModel().getValueAt(modelRow, 0);
              if(sMultip.lastIndexOf("*") != -1) {
                  int multip = Integer.parseInt(sMultip.substring(sMultip.lastIndexOf("*")+1,sMultip.length()));
                  if(player.money.getMoney() &gt;= price) {
                      player.money.deduct(price);
                      worker.multiplyMultiplier(multip);
                      ((DefaultTableModel)table.getModel()).removeRow(modelRow);  
                  }
              } else if(sMultip.lastIndexOf("+") != -1) {
                  int multip = Integer.parseInt(sMultip.substring(sMultip.lastIndexOf("+")+1,sMultip.length()));
                  if(player.money.getMoney() &gt;= price) {
                      player.money.deduct(price);
                      worker.plusMultiplier(multip);
                      ((DefaultTableModel)table.getModel()).removeRow(modelRow);
                  }
              }
              progressBar1.setString(worker.getProfit()+"$");
              lblMultiplier.setText(worker.multiplier+"x");
          }
      };
  
      ButtonColumn buttonColumn = new ButtonColumn(table, upgradeMultiplier, 1);
      buttonColumn.setMnemonic(KeyEvent.VK_D);
  
      scrollPane.setViewportView(table);
  
      JLabel lblMultiplierStatic = new JLabel("Multiplier:");
      lblMultiplierStatic.setBounds(17, 9, 55, 16);
      frame.getContentPane().add(lblMultiplierStatic);
  
      lblMultiplier = new JLabel("1x");
      lblMultiplier.setBounds(83, 9, 43, 16);
      frame.getContentPane().add(lblMultiplier);
  
      JButton btnNewButton = new JButton("New button");
      btnNewButton.addActionListener(new ActionListener() {
          public void actionPerformed(ActionEvent e) {
              timerStop = true;
          }
      });
      btnNewButton.setBounds(65, 185, 98, 26);
      frame.getContentPane().add(btnNewButton);
  
  
  }
  }
  </code></pre>
  
  <p>Look on the top of <code>GameWindow</code>, I change value of gameStarted after closing frame GameWindow and it changes it to false so it should stop the timer... but ... not.</p>