problem with Countdown Timer
hello there
in the following code the timer uses the default values for the hour,minute,second
and i want to use specific values like if i want to enter just only 30 minutes without entering the hours and the seconds
how to do so?
and how to execute an action when the counter be 0:0:0 (When The Counter Finishes)?
this is the code:
import java.awt.*;
import java.math.*;
import java.util.*;
import java.text.*;
import javax.swing.*;
import java.awt.event.*;
publicclass Timerextends JFrame{
JButton start =new JButton("Start");
JButton stop =new JButton("Stop");
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 Timer(){
super("Timer");
addWindowListener(new WindowAdapter(){
publicvoid 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);
start.addActionListener(new ActionListener(){
publicvoid actionPerformed( ActionEvent e ){
try{
date=timef.parse(time.getText());
startT=date.getTime();
}catch (Throwable fe){startT=0;}
startT+=System.currentTimeMillis();
timer.start();
}
}
);
stop.addActionListener(new ActionListener(){
publicvoid actionPerformed( ActionEvent e ){
timer.stop();
}
}
);
time.setBounds(150,30,100,24);
time.setOpaque(true);
time.setBackground(Color.pink);
getContentPane().add(stop);
getContentPane().add(start);
getContentPane().add(time);
timer =new javax.swing.Timer(1000,new ActionListener(){
publicvoid actionPerformed(ActionEvent e){
stopT = System.currentTimeMillis();
time.setText(timef.format(new Date(startT-stopT)));
}
}
);
setVisible(true);
}
publicstaticvoid main (String[] args){
UIManager.put("Label.font" ,new Font("Times New Roman",0,23));
UIManager.put("Button.font",new Font("Arial",1,21));
new Timer();
}
}

