title: How to call a Swing Timer from Inside a JButton
body: <p>I'm trying to start a Timer after a button is pressed.</p>
  
  <p>The following code without a button works</p>
  
  <pre><code>    Timer timer = new Timer(1000, new ActionListener() {
          public void actionPerformed(ActionEvent e) {
              System.out.println("test");
          }
      });
      timer.start();
      try {
          Thread.sleep(10000);
      } catch (InterruptedException e) {
          e.printStackTrace();
      }
  </code></pre>
  
  <p>However when I put it inside a button</p>
  
  <pre><code>    startButton.addActionListener(new ActionListener() {
          public void actionPerformed(ActionEvent evt) {
              Timer timer = new Timer(1000, new ActionListener() {
                  public void actionPerformed(ActionEvent e) {
                      System.out.println("test");
                  }
              });
              timer.start();
              try {
                  Thread.sleep(10000);
              } catch (InterruptedException e) {
                  e.printStackTrace();
              }
          }
      });
  </code></pre>
  
  <p>It does not print anything when the button is pressed.</p>
  
  <p>I thought this occurred because the main thread ends, so I tried adding a <code>while (true)</code> at the end, but it did not change anything.</p>
  
  <p>Thanks</p>