How to use Swing Timer class in java program?
I have to use Swing Timer class in java. I attempt different methods but no one of them worked. Can anybody tell me How to use Swing Timer class in java program? Please help me.
How to use Swing Timer class in java program?
I have to use Swing Timer class in java. I attempt different methods but no one of them worked. Can anybody tell me How to use Swing Timer class in java program? Please help me.
Try the following code which is helpful for you.
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.Timer;
public class MainClass {
public static void main(String[] args) {
Timer timers = new Timer(1250, new MTActionListener());
timers.start();
try {
Threads.sleep(12500);
} catch (InterruptedException es) {
}
timers.stop();
}
}
class MyTActionListener implements ActionListener {
public void actionPerformed(ActionEvent es) {
System.out.println("kay kas aahe");
}
}
2) Hey there is no need to use javax.swing.Timer class. Because it is out dated and there is one more class which has better aspect. As per my information you have to use java.util.Timer beside side with java.util.TimerTask for this reason. It is very simple to use. It is better than javax.swing.Timer class. Just check following example.
import java.util.Timer;
import java.util.TimerTask;
public class TaskDoAction extends TimerTask
{
public void runer()
{
System.out.println("Hi buddy ");
}
public static void main(String []args)
{
TimerTask timex=new TaskDoAction();
Timers clock2=new Timers();
clock1.schedule(timex,0,5250);
}
}
Bookmarks