title: Swing Timer in Netbeans GUI
body: <p>This is my first Swing application so I used NetBeans GUI Builder. 
  There is a Start button to start simulation(two loops: the outer by days and the inner by hours). At each iteration the loop should pause. When you click on the Stop button simulation should stop. I tried to ways:
  1)</p>
  
  <pre><code>private void StartActionPerformed(java.awt.event.ActionEvent evt) {                                      
      Timer timer = new Timer(5000, new ActionListener() {
  
          @Override
          public void actionPerformed(ActionEvent e) {
              while(presentDay &lt;= numberOfDays){
                  jTextField2.setText(Integer.toString(presentDay));
                  for(; presentTime &lt;= 18; presentTime++){
                      jTextField6.setText(Integer.toString(presentTime));
  
                      StringBuilder res = new StringBuilder();
  
                      for(Event ev: schedule[presentDay-1].day){
                          if((presentTime &lt;= ev.getTimeFinish()) &amp;&amp; (presentTime &gt;= ev.getTimeStart())){
                              res.append(ev);
                              ev.setStatus(1);
                          }
                      }
                      currentEvent.setText(res.toString());
                      // Pause should here
                  }
                  presentTime = 9;
                  presentDay++;
              }
          }
      });
      timer.start();
  }
  </code></pre>
  
  <p>2) Add Timer in main class constructor</p>
  
  <pre><code>public PlanningSystem() {
      initComponents();
      ...
      timer = new Timer(5000, new ActionListener() {
  
          @Override
          public void actionPerformed(ActionEvent e) {
              while(presentDay &lt;= numberOfDays){
                  jTextField2.setText(Integer.toString(presentDay));
                  for(; presentTime &lt;= 18; presentTime++){
                      jTextField6.setText(Integer.toString(presentTime));
  
                      StringBuilder res = new StringBuilder();
  
                      for(Event ev: schedule[presentDay-1].day){
                          if((presentTime &lt;= ev.getTimeFinish()) &amp;&amp; (presentTime &gt;= ev.getTimeStart())){
                              res.append(ev);
                              ev.setStatus(1);
                          }
                      }
                      currentEvent.setText(res.toString());
                      // Pause here
                  }
                  presentTime = 9;
                  presentDay++;
              }
          }
      });
  }
  private void StartActionPerformed(java.awt.event.ActionEvent evt) {                                      
  
      timer.start();
  }
  </code></pre>
  
  <p>But both ways did not work. I hope you help solve this problem.
  And sorry for my English :)</p>
  
  <p>======================================================================
  After editing :</p>
  
  <pre><code>private void StartActionPerformed(java.awt.event.ActionEvent evt) {                                      
  if(presentDay &lt;= numberOfDays){
      jTextField2.setText(Integer.toString(presentDay));
      Timer tim = new Timer (5000, new ActionListener() {
          @Override
          public void actionPerformed(ActionEvent e) {
          if(presentTime &lt;= 18){
              jTextField6.setText(Integer.toString(presentTime));
              StringBuilder res = new StringBuilder();
              for(Event ev: schedule[presentDay-1].day){
                  if((presentTime &lt;= ev.getTimeFinish()) &amp;&amp; (presentTime &gt;= ev.getTimeStart())){
                      res.append(ev);
                      ev.setStatus(1);
                  }
              }
              currentEvent.setText(res.toString());
              presentTime++;
          }
          }
      });            
      tim.start();
      presentTime = 9;
      presentDay++;
  }
  }
  </code></pre>