Timer, Again

Sorry but I have to post another topic on timers. I had a look at threads on timers but could not see a solution to my specific problem.

This is what I'm trying to replicate. In Borland Visual C++ they have a Timer wrapper. It's simply a matter of putting your code inside it. It runs the code every x seconds, depending on what value you put on its interval property.

I want to do this in Java. I need at least 2 Timer objects, each having different intervals. One would be used to display time (so the interva here is once every second), while the other one is user selectable.

The Timer in java.util will do the job for you. Put the code you want to execute in the run() method of a little class that implements TimerTask. Create a timer, and schedule your tasks to run at a fixed rate.

Did you try some things with Timer and run into trouble? Post some almost-running code and see if we can help. It ought to look like:

A personal style note on the suggestion above ... I'd avoid giving one of my classes the same name as something in the JDK libraries. MyCustomTimer might be better than Timer if it really is Timer-like.

A good question is never answered. It is not a bolt to be tightened into place but a seed to be planted and to bear more seed toward the hope of greening the landscape of the idea. John Ciardi

basically the timer will call the funcion actionPerformed of the ActionListener you provided in the constructor of the timer after xxxx millisecond. At the end I added the while(true); because it is runned in the console and once the main is finish the application exit. If you use it in a swing(or awt) app where there is already a thread doing that you do not need it!

Enjoy!

I’ve looked at a lot of different solutions, and in my humble opinion Aspose is the way to go. Here’s the link: http://aspose.com