Skip to Main Content

New to Java

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

how to prevent timer from repeats

807600Jun 18 2007 — edited Jun 18 2007
hello there i've made a countdown timer
and i want it when it finishes counting till 0 not repeating from the beginning but do some action
i uesd the method
timer.setRepeats(false);
but it stops the timer from counting
i'm using javax.swing.timer;
and here is the code:
import java.math.*;
import java.util.*;
import java.text.*;
import javax.swing.*;
import java.awt.event.*;
import java.awt.*;
public class Timer1 extends JFrame {
   JComboBox combo = new JComboBox();
   JButton start = new JButton("Start");
   JButton stop = new JButton("Stop");
   JButton clear = new JButton("Clear");
   JTextField time = new JTextField("");
   javax.swing.Timer timer;
   SimpleDateFormat timef = new SimpleDateFormat("HH:mm:ss",Locale.getDefault());
   long startT,stopT;
   Date date=new Date();
   public Timer1() {
      super("Timer");
      timef.setTimeZone(TimeZone.getTimeZone("GMT"));
      addWindowListener(new WindowAdapter() {
         public void windowClosing(WindowEvent ev) {
            dispose();
            System.exit(0);
         }
      }
      );
      setBounds(10,10,400,200);
      getContentPane().setLayout(null);
      start.setBounds(10,30,100,24);
      start.setBackground(Color.green);
      stop.setBounds(10,60,100,24);
      stop.setBackground(Color.red);
      combo.setBounds(10,90,100,25);
      combo.setBackground(Color.blue);
      clear.setBounds(150,60,100,24);
      clear.setBackground(Color.green);
      combo.addItem("1 Hour");
      combo.addItem("2 Hours");
      combo.addItem("3 Hours ");
      combo.addItem("4 Hours");
      combo.addItem("5 Hours");
      combo.addItem("6 Hours");
       combo.addItem("7 Hours");
      combo.setSelectedIndex(0);
      start.addActionListener(new ActionListener() {
         public void actionPerformed( ActionEvent e ) {
            try {
                 startT=(combo.getSelectedIndex()+1)*60*60*1000;  // change if combo box values changed
            }
            catch (Throwable fe) {
               startT=0;
            }
            startT+=System.currentTimeMillis();
            timer.start();
         }
         
      }
      );
      stop.addActionListener(new ActionListener() {
         public void actionPerformed( ActionEvent e ) {
            timer.stop();
         }
      }
      );
      clear.addActionListener(new ActionListener() {
         public void actionPerformed( ActionEvent e ) {
            time.setText("");
         }
      }
      );
      time.setBounds(150,30,100,24);
      time.setOpaque(true);
      time.setBackground(Color.pink);
      getContentPane().add(stop);
      getContentPane().add(start);
      getContentPane().add(time);
      getContentPane().add(combo);
      getContentPane().add(clear);
      timer = new javax.swing.Timer(1000, new ActionListener() {
         public void actionPerformed(ActionEvent e) {
            stopT = System.currentTimeMillis();
            time.setText(timef.format(new Date(startT-stopT)));
         }
      }
      );
      setVisible(true);
   }
 
   public static void main (String[] args) {
      UIManager.put("Label.font" ,new Font("Times New Roman",0,23));
      UIManager.put("Button.font",new Font("Arial",1,21));
      new Timer1();
   }
}
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on Jul 16 2007
Added on Jun 18 2007
20 comments
531 views