title: Java - Updating a GUI made in Swing
body: <p>I am trying to create a simple GUI form which has only 2 elements - a simple label and a button. The text displayed on button is 'Start'. The label is displaying 0 by default. </p>
  
  <p>When I click Start button following actions shall take place:</p>
  
  <ol>
  <li>Counter shall start incrementing by 1 from 0 at every 1 second.</li>
  <li>Text displayed on the Start button shall change to Stop.</li>
  <li>When again I click on the same button (now showing caption as Stop), increment shall stop. </li>
  <li>Text on the button shall change to Start. And so on...</li>
  </ol>
  
  <p>I am developing my application in Netbeans. </p>
  
  <p>As shown in the above diagram, there are 2 .java files</p>
  
  <p>Contents of AGC.java are:</p>
  
  <pre><code>public class AGC extends javax.swing.JFrame 
  {
      public AGC()
      {    
          initComponents();
      }
  
      public static void main(String args[])
      {
          java.awt.EventQueue.invokeLater(new Runnable() {
              public void run() 
              {
                  new AGC().setVisible(true);
              }
          });
      }
  
      private javax.swing.JButton btnStartStop;  // name of start stop button
      private javax.swing.JLabel lblCounter;   // name of the label
  
  }
  </code></pre>
  
  <p>Contents of Main.java are:</p>
  
  <pre><code>public class Main 
  {
      public static int count = 0;
      public static boolean started = false;
  }
  </code></pre>
  
  <p>I want to implement following logic:</p>
  
  <pre><code>private void btnStartStopMouseClicked(java.awt.event.MouseEvent evt) 
  {
      if (Main.stared == true)
      {
          // logic to start counting
      }
      else
      {
          // logic to stop counting
      }
  }
  </code></pre>
  
  <p>My problem is this:</p>
  
  <ol>
  <li>How to update lblCounter at every 1 second? </li>
  <li>What logic shall I implement to start the timer of 1 second and how to access lblCounter in that method ?</li>
  </ol>
  
  <p>Kindly help. A working code would be very highly appreciated. Thanks in advance.</p>
  
  <p>Jay</p>