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();

}

}

[4751 byte] By [First_knighta] at [2007-11-27 7:56:33]
# 1

I see a problem right off the bat.

1: you import java.util.*

2: you declare your class a public class Timer

there is a java.util.Timer class that already exists. I'd imagine that your compiler is screaming about some sort of disambiguation.

I showed you that (java.util.Timer) in the thread you started a few minutes ago related to this very same topic.

Don't crosspost. It will only give you negative points on these forums. (Not to mention the fact that it really pisses me off.)

Navy_Codera at 2007-7-12 19:38:10 > top of Java-index,Java Essentials,New To Java...
# 2
> I see a problem right off the bat.> 1: you import java.util.*> 2: you declare your class a public class> TimerThere is a javax.swing.Timer as well. ;-)
CaptainMorgan08a at 2007-7-12 19:38:10 > top of Java-index,Java Essentials,New To Java...
# 3
thanks for advice i'll look for thatbut would you please help me adjusting this codeyour help woulde be appreciatedthanks again
First_knighta at 2007-7-12 19:38:10 > top of Java-index,Java Essentials,New To Java...
# 4
could any one help me with this code please
First_knighta at 2007-7-12 19:38:10 > top of Java-index,Java Essentials,New To Java...
# 5
i need urgent help please
First_knighta at 2007-7-12 19:38:10 > top of Java-index,Java Essentials,New To Java...
# 6
Oh it's urgent, then in that case just sit back and all your work will be done for you because you are soooooo much more important than anyone else. Right?
floundera at 2007-7-12 19:38:10 > top of Java-index,Java Essentials,New To Java...
# 7
no sir i didn't mean that but i said so because i have spent hours searching to adjust the code but in vainthank you sir
First_knighta at 2007-7-12 19:38:10 > top of Java-index,Java Essentials,New To Java...
# 8
You are most welcome!
floundera at 2007-7-12 19:38:10 > top of Java-index,Java Essentials,New To Java...
# 9

looks like all you need to do is create 4 variables

hour, min, sec (you specify) and counter (a calculation of number of secs in hour,min,sec)

your timer code:

1) uses 'counter' to recalculate # of hours, # of minutes, # of seconds

2) outputs the display as (eg) hour+":"+min+":"sec

(tip - use DecimalFormat() to ensure numbers < 10 appear as 01 instead of 1)

3) decrement counter

4) a check of counter value - if <= 0 stop the timer

(this part depends on where the check is, in relation to counter--)

Michael_Dunna at 2007-7-12 19:38:10 > top of Java-index,Java Essentials,New To Java...
# 10
thanks mr Michael_Dunn but would you please post a code to explainbecause i'm so new to java and didn't understand how to do most fo what you saidthanks any way
First_knighta at 2007-7-12 19:38:10 > top of Java-index,Java Essentials,New To Java...
# 11
Sod off doofus and do your own work.
floundera at 2007-7-12 19:38:10 > top of Java-index,Java Essentials,New To Java...
# 12

> because i'm so new to java

if you ever want that to change, put your thinking cap on.

just try to do this bit - doesn't need a gui, it's just simple math

counter (a calculation of number of secs in hour,min,sec)

then the reverse

uses 'counter' to recalculate # of hours, # of minutes, # of seconds

have a go, but if you strike a problem post again with the specific problem,

along with the code you've tried

Michael_Dunna at 2007-7-12 19:38:10 > top of Java-index,Java Essentials,New To Java...