3: Java Swing Timer Netbeans Tutorial

The third Tutorial directs us on Timers. Timers are often needed and in use and we need them. The fastest way in a Swing Application to build a secure timer is always to use a Java Swing timer. In my example App I generated a Netbeans-Ant-Java project (like always when generating with enabled “generate Main”!!

I gave this application the name TimerExample. In my timer.example package I got now the main class TimerExample. In this jar file wanted the timer and the timer shall be started by the main method.

To generate a Timer I used the code:

public static Timer timerexample = new Timer(1000,(e) → {

    System.out.println("testtimer runs");
    
    
    
});

but what is this code doing? Simple! I was generating a new Timer from Java Swing Timer class with the name timer example. with the line Timer timer example = new Timer(interval ms, Action event) I can define the timer.

Inside I was placing the System.out… line for showing the running timer in the terminal. n this case the Timer has it’s running method generated with a reflection (1000, (e) → { code to do });

So the timer is ready for use. Now I started the timer in the main method. While I have no Gun I had to build a loop that the program will not end immediately:
int i=0;
while(i==0){
timerexample.start();
}

In this situation the main method starts the timer and every 1000 ms it prints the line “testier runs”. The entire Class looks like this in the IDE:

Like you can see I imported Java.swing.Timer so I have the package to use it. That’s all to use a Java Swing Timer.

Love these tutorials! Because of these, I have pretty much decided to replace Xojo with Java as my main language.

I have been playing with IntelliJ and a demo version of JFormDesigner but I have found the Netbean tutorials useful as well as I have not completely decided on which IDE to use.

I can work with both, Netbeans and IntelliJIdea but I love Netbeans more. I have both and also JFormdesigner. It is like all time in our live a question of taste. Technically they can do both the same in the same quality and with the same comfort.