I used netbeans to generate a JFrame and I want to add a TickTimer to it (A count down from 10 to 1), my question is: where do I add my input for such a thing? and where do I add the static class in which I'll extend the TimerTask class? in the main or in the constructor before the initComponents() or with the fields? I might be getting it wrong so if you could write me a little simple program that does this even if you do it with Timer not TimerTask I would be thankful.
Asked
Active
Viewed 223 times
0
-
Don't use a `TimerTask`, use a [`javax.swing.Timer`](https://docs.oracle.com/javase/tutorial/uiswing/misc/timer.html) instead. – MadProgrammer Apr 12 '15 at 10:03
-
@MadProgrammer I've been searching the internet for how but couldn't find it, most people use Timer just for one scheduled Task, could you tell me how? – Kasem Hato Apr 12 '15 at 10:11
-
The link in comment takes you to the tutorial. You want to to a count timer in Swing, a Swing Timer is the simplest and safest way to achieve it – MadProgrammer Apr 12 '15 at 10:24
-
Maybe something like [this](http://stackoverflow.com/questions/14678750/java-label-timer-and-saving/14678873#14678873) or [this](http://stackoverflow.com/questions/27009069/java-timers-and-actionlisteners/27009144#27009144) or [this](http://stackoverflow.com/questions/21272833/multiple-swing-timers-cause-multiple-actions-to-happen/21272994#21272994) – MadProgrammer Apr 12 '15 at 10:27
1 Answers
0
After going through some articles finally found the answer and here you go if you have the same problem (Making a count down timer using Timer:
public class example extends JFrame implements ActionListener{
javax.swing.Timer timer = new javax.swing.Timer(1000, this);
int c = 10;
public example(){
timer.start();
initComponents(); // this is auto generated when creating JFrame
@Override
public void actionPerformed(ActionEvent e) {
jLabel1.setText(""+c--);
if (c == -1) {
timer.stop();
}
}
}

Kasem Hato
- 53
- 6