I am really confused with the Java swing timer. Does it need an action listener? Could someone please give me an example? Thanks!
-
3http://docs.oracle.com/javase/tutorial/uiswing/misc/timer.html – Quinma Nov 21 '12 at 23:36
-
*"I am really confused with the.."* ..way SO works? A tip. Do some research and show your effort. SO is not a help-desk or your personal tutor. Voting to close as 'not a real..'. – Andrew Thompson Nov 21 '12 at 23:45
-
I have done research, it is ok to have confusion when it comes to programming. I am self taught and do lots of research before I ask a question. – Nov 21 '12 at 23:46
-
*"do lots of research before I ask a question."* So show it. Don't presume we can read your mind and see what you've tried. It helps to state what you have found out so people don't have to repeat themselves. It helps to state where you found it out so that people can see 'Java Tutorial' good, 'Rose India' bad.. – Andrew Thompson Nov 21 '12 at 23:50
-
1Consider showing [current] code - makes these sorts of questions go better :) – Nov 21 '12 at 23:51
-
*Could someone please give me an example* ... what was wrong with the example in the Swing tutorial as linked by @Quinma or all other Timer examples you can find here on SO ? The Swing tag wiki links to a Timer example as del – Robin Nov 22 '12 at 07:38
2 Answers
Here is an example of using javax.swing.Timer
:
import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.BorderFactory;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;
import javax.swing.Timer;
public class SimpleTimer extends JFrame
{
private JLabel label;
private Timer timer;
private int counter = 10; // the duration
private int delay = 1000; // every 1 second
private static final long serialVersionUID = 1L;
public SimpleTimer()
{
super("Simple Timer");
setDefaultCloseOperation(EXIT_ON_CLOSE);
label = new JLabel("Wait for " + counter + " sec", JLabel.CENTER);
JPanel contentPane = (JPanel) getContentPane();
contentPane.add(label, BorderLayout.CENTER);
contentPane.setBorder(BorderFactory.createEmptyBorder(5, 5, 5, 5));
pack();
ActionListener action = new ActionListener()
{
@Override
public void actionPerformed(ActionEvent event)
{
if(counter == 0)
{
timer.stop();
label.setText("The time is up!");
}
else
{
label.setText("Wait for " + counter + " sec");
counter--;
}
}
};
timer = new Timer(delay, action);
timer.setInitialDelay(0);
timer.start();
setVisible(true);
}
public static void main(String[] args)
{
SwingUtilities.invokeLater(new Runnable()
{
@Override
public void run()
{
new SimpleTimer();
}
});
}
}

- 115,165
- 71
- 313
- 417
-
-
@user1832583 `this` was pointing to the current instance of `SimpleTimer` which extends `ActionListener`. I updated my answer to be clearer. – Eng.Fouad Nov 21 '12 at 23:47
Setting up a timer involves creating a Timer object, registering one or more action listeners on it, and starting the timer using the start method. For example, the following code creates and starts a timer that fires an action event once per second (as specified by the first argument to the Timer constructor). The second argument to the Timer constructor specifies a listener to receive the timer's action events. Timers are constructed by specifying both a delay parameter and an ActionListener. (source)
Futhermore, the timer's timing is done in thread distinct from the event dispatch thread (or EDT) which is the thread that runs the code in the ActionListener. So even if the actionPerformed code is slow, the timer will keep on firing regardless and will queue its actionPerformed code on the event queue which will likely get backed up and the event thread will get clogged and the application will be unresponsive or poorly responsive, unsell you set coalesce to true
(source).
Here is a good tuturial How to Use Swing Timers

- 1
- 1

- 47,137
- 25
- 94
- 117
-
*"which will likely get backed up and the event thread will get clogged and the application will be unresponsive or poorly responsive"* unless you set coalesce to `true` ;) +1 for mentioning the EDT though – MadProgrammer Nov 21 '12 at 23:56
-