# 3: Java Swing Timer Netbeans Tutorial

**URL:** <https://ifnotnil.com/t/3-java-swing-timer-netbeans-tutorial/2050>\
**Category:** Java\
**Tags:** java, tutorials\
**Created:** [14 September 2021 20:36 UTC](https://ifnotnil.com/t/3-java-swing-timer-netbeans-tutorial/2050 "2021-09-14T20:36:22Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![thorstenstueker](https://ifnotnil.com/letter_avatar_proxy/v4/letter/t/aca169/32.png) [@thorstenstueker](https://ifnotnil.com/u/thorstenstueker)\
**Post date:** [14 September 2021 20:36 UTC](https://ifnotnil.com/t/3-java-swing-timer-netbeans-tutorial/2050/1 "2021-09-14T20:36:22Z")

</div>

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:

 ![image](https://ifnotnil.com/uploads/default/original/2X/a/a5e9bb28ab0087b9dab98890a30c76b7ed8bb513.jpeg)

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.

---

<div class="post-metadata">

**Author:** ![pjsmith67](https://ifnotnil.com/user_avatar/ifnotnil.com/pjsmith67/32/5028_2.png) [@pjsmith67](https://ifnotnil.com/u/pjsmith67)\
**Post date:** [15 September 2021 15:03 UTC](https://ifnotnil.com/t/3-java-swing-timer-netbeans-tutorial/2050/2 "2021-09-15T15:03:59Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![thorstenstueker](https://ifnotnil.com/letter_avatar_proxy/v4/letter/t/aca169/32.png) [@thorstenstueker](https://ifnotnil.com/u/thorstenstueker)\
**Post date:** [15 September 2021 15:08 UTC](https://ifnotnil.com/t/3-java-swing-timer-netbeans-tutorial/2050/3 "2021-09-15T15:08:35Z")

</div>

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.
