Java Timer

J

Jesse

Dear all,

i am a new to java programmer and i need some help!

can someone help me with a example of a java Applet count down timer ??

Thank u in forward,,,

Marry X- Mas and a Happy New year !!!

Jesse
(e-mail address removed)
 
E

E.Otter

I'm guessing you are talking about the javax.swing.Timer which basically
"fires" an action event at specific time intervals until the stop() method
is called on it. There is also a java.util.Timer class but that is more
complex. Not for a java novice. Read the API docs on javax.swing.Timer
especially these links which are referenced in those API docs.

"how to" on javax.swing.Timer
http://java.sun.com/docs/books/tutorial/uiswing/misc/timer.html

info on both
http://java.sun.com/products/jfc/tsc/articles/timer/


This is an japplet example that uses javax.swing.Timer
import javax.swing.*;
import java.awt.event.*;

public class MyApplet extends JApplet {
private Timer timer;
private JTextField display;
private int counter=0;

public void init() {
display= new JTextField(10);
getContentPane().add(display);
}
public void start() {
if(timer!=null) {
timer.restart();
} else {
ActionListener timerHandler = new TimerHandler();
//triger the timer event every 1000 milliseconds
timer = new Timer(1000,timerHandler);
}
}
public void stop() {
timer.stop();
}
private class TimerHandler implements ActionListener {
public void actionPerformed(ActionEvent e) {
//this event occurs within the Swing event-handler thread so
//should be safe to modify visible/paintable info of a Swing
class
display.setText("Counter is "+ ++counter);
}
}
}
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top