Introduction on Timer in Java Swing
The word “timer” is self-defined, and “Swing” in java is useful to develop window-based applications. SO using the timer facility of java swing, we can develop window-based cool timer applications. Timer in Java Swing in this article, we will see how we can use this package by different examples.
How to use Timer in Java Swing?
It is a very useful facility that helps us implement timers for some actions to have happened in specified intervals or implement repetitive tasks along with timer running. Swing timers are very handy. You just need to invoke any action event associated with the timer, and that action will be continued until the timer ends. Now the question is, how will the timer end? It is also in your hands. You just need to specify the time in milliseconds, and that is all. The specified action event will automatically be stopped once the timer ends.
Examples of Timer in Java Swing
We will discuss some code examples of Timer in Java Swing here. I advise you to read the code line by line, as I have mentioned inline comments in code to get better readability and understanding.
Example #1
In this example, our objective to start a timer with any message displayed, and the message will only be the output until the timer ends and we see the “Timeout” message displayed, and the program ends. The display of a message is an event of action that is fired by the swing timer. After the timer ends, the message of timeout is displayed, indicating the end of the swing timer. You should go through the code carefully with inline comments to understand how it is implemented.
Code:
import java.awt.event.*;
import javax.swing.*;
public class SwingTimerDemo {
static public void main(String [] args) {
try{
ActionListener ticktock = new ActionListener() {
public void actionPerformed(ActionEvent evnt) {
System.out.println("Swing timer started"); //display of this message is basically an action which is associated with swing timer until timer stops
}
};
Timer timer = new Timer(555 ,ticktock); //timer is ticking
timer.setRepeats(false); //by using this, we are asking to off timer once
timer.start();
Thread.sleep(5555);
System.out.println("Timeout"); //timer ends and this message is displayed
} catch (InterruptedException expn) {
}
}
}
Output:
In the above picture, you can see the timer started in the output console. After some time, you will see the message “Timeout” printed in output indicating that the program is completed.
Final Output:
Example #2
Here we will take an example where we will create a window inside which there will be a button where the timer runs. After the end of the timer, a close window message will be displayed indicating the end of the timer. Our objective is to start a timer with an action event as the creation of a window with a button and associated message displayed inside the button, which is changing as time progresses. So, we will also create a window using JFrame here so that we can create a time changing labeled button inside it. The display of a message is an event of action that is fired by the swing timer. After the timer ends, the message of timeout is displayed, indicating the end of the swing timer. Follow the inline comments of the code for readability and understanding.
Code:
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
class TimerWindow extends JFrame implements ActionListener{ //creation of an window using JFrame
private int start = 1;
private JButton jbtn; //creation of button inside the JFrame window
private Timer swingtimer; //swing timer instance
TimerWindow(int tm)
{
start += tm;
setTitle("Timer Window");
setLayout(new FlowLayout());
setTimer();
setSize(700,350);
setVisible(true);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
private void setTimer()
{
jbtn = new JButton("Staring Timer...");
add(jbtn);
swingtimer = new Timer(2222,this);
swingtimer.start();
}
public void actionPerformed(ActionEvent evnt)
{
start--;
if(start>=1)
{
jbtn.setText("Time : "+start); //changing the label of button as the timer decrases
}else{
jbtn.setText("Timeout... Now,Close the Window");
swingtimer.stop();
}
}
}
public class TimerInSwing {
static public void main(String[] args) {
TimerWindow tw = new TimerWindow(5);
}
}
Output:
After running the above program, you will see a frame that will pop out. Inside the window (which is named “Timer Window”), you will see a button labeled as “Starting Timer”.
As time progresses, the timer starts and the label of the button changes with time like “Time: 5”, then Time: 4”, and so on. See below sample screenshot.
Finally, the timer stops, and the button label also changed as we defined in the code. Below is its screenshot. You can now close the window as the program finishes.
Example #3
In this example, we will see how we can trigger an action in terms of displaying repeated messages as long as the timer continues. Timeout will be displayed when the timer stops. You are advised to go through the code with the inline comments for better understanding.
Code:
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.Timer;
public class TimerInSwingDemonstration {
static public void main(String[] args) {
Timer swingtimer = new Timer(1111, new SwingTimerActionListener());
swingtimer.start(); //timer started
try {
Thread.sleep(5555);
} catch (InterruptedException e) {
}
swingtimer.stop();
System.out.println("Timeout");
}
}
class SwingTimerActionListener implements ActionListener {
public void actionPerformed(ActionEvent evnt) {
System.out.println("Timer is ticking"); //action event is triggered by display of message
}
}
Output:
Conclusion
This concludes our learning of the topic “Timer in Java Swing”, as you can see how we had triggered action of events exploiting the timer facility of java swing. Write yourself the codes mentioned in the above examples in your machine’s java compiler and verify the output. Learning of codes will be incomplete if you will not write code by yourself.
Recommended Articles
This is a guide to Timer in Java Swing. Here we discuss the basic concept, how to use Timer in Java Swing, along with the examples of Java Swing. You can also go through our other suggested articles to learn –