title: Create a count up timer in java (seconds and milliseconds only)
body: <p>I'm doing a <strong>count up timer in java</strong>. I found the following example: <a href="https://stackoverflow.com/questions/5745745/creating-a-count-up-timer-to-break-in-java">Creating a Count Up timer to Break in java</a>, and that's working fine. But I want to show the time in the <strong>format "mm:ss:SSS"</strong>, because I want to measure really small time responses. So, when it reaches the 1000 msec, it is 1 second.</p>
  
  <p>I made some changes, but I can't present the time in the pretended format. The numbers start to appear were the seconds should be, and not in the milliseconds.</p>
  
  <p>If you have a better example that the one that I'm following, it's fine.</p>
  
  <p><strong>Edit:</strong> It is better, but it is not working fine yet. It is counting too slow (1 minute here correspond to 2 real minutes). My code is here:</p>
  
  <pre><code>public class Counter extends JFrame {
  
      private static final String stop = "Stop";
      private static final String start = "Start";
      private final ClockListener clock = new ClockListener();
      private final Timer timer = new Timer(1, clock);
      private final JTextField tf = new JTextField(9);
  
  public Counter() 
  {
      timer.setInitialDelay(0);
  
      JPanel panel = new JPanel();
      tf.setHorizontalAlignment(JTextField.RIGHT);
      tf.setEditable(false);
      panel.add(tf);
      final JToggleButton b = new JToggleButton(start);
      b.addItemListener(new ItemListener() 
      {
          @Override
          public void itemStateChanged(ItemEvent e) 
          {
              if (b.isSelected()) 
              {
                  timer.start();
                  b.setText(stop);
              } 
              else 
              {
                  timer.stop();
                  b.setText(start);
              }
          }
      });
      panel.add(b);
  
      this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
      this.add(panel);
      this.setTitle("Timer");
      this.pack();
      this.setLocationRelativeTo(null);
      this.setVisible(true);
  }
  
  private class ClockListener implements ActionListener
  {
      private int minutes;
      private int seconds;
      private int milliseconds;
  
      @Override
      public void actionPerformed(ActionEvent e) 
      {
          SimpleDateFormat date = new SimpleDateFormat("mm.ss.SSS");
  
          if (milliseconds == 1000) 
          {
              milliseconds = 000;
              seconds++;
          }
          if (seconds == 60) {
              seconds = 00;
              minutes++;
          }
  
          tf.setText(String.valueOf(minutes + ":" + seconds + ":" + milliseconds));
          milliseconds++;
      }
  }
  
  public static void main(String[] args) 
  {
      EventQueue.invokeLater(new Runnable() 
      {
          @Override
          public void run() {
              Counter clock = new Counter();
              clock.start();
          }
      });
    }
  }
  </code></pre>