can anyone help me to put a countdown timer in a dialog box. i want to prompt the user when the time expires. tnx!
stultuske 1,116 Posting Maven Featured Poster
ehm ... a JLabel you update.
JamesCherrill 4,733 Most Valuable Poster Team Colleague Featured Poster
Use a javax.swing.Timer
When the timer expires it runs its actionPerformed method, where you can display a message or whatever you like.
See the API doc for details
Xabush 0 Newbie Poster
Even though it is to late to post it now, here a simple countdown timer I created in my spare time. Feel free to review the code and tell me if there is anything wrong.
import java.awt.event.*;
import java.awt.*;
import java.util.concurrent.Executor;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import javax.swing.*;
/**
* Created by Xabush on 2/19/14.
* Project: JavaProgramming @ 12:20 AM
*/
public class TimerClass extends JPanel implements Runnable{
private int hours;
private int minutes;
private int seconds;
private JLabel timerLabel;
private Color originalColor;
private Timer secTimer;
private boolean started = false;
private int initSec, initMin, initHr;
private JButton startButton;
private JButton stopButton;
private JButton resetButton;
private JButton addButton;
private JButton newButton;
public TimerClass(){
setLayout(new BorderLayout());
JPanel panel1 = new JPanel(new GridLayout(1,3,7,7));
JPanel panel2 = new JPanel(new GridLayout(1,3,4,4));
timerLabel = new JLabel("00 : 00 : 00");
originalColor = timerLabel.getForeground();
secTimer = new Timer(1000,new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
if(started){
if(seconds == 0 && minutes == 0 && hours == 0){
timerLabel.setForeground(originalColor);
JOptionPane.showMessageDialog(TimerClass.this,"Time Up","Time's Up!",JOptionPane.INFORMATION_MESSAGE);
secTimer.stop();
addButton.setEnabled(true);
}
//Change the color when 5 seconds remain
if((seconds <= 5 && seconds > 0) && minutes == 0 && hours == 0){
timerLabel.setForeground(Color.RED);
}
if(seconds > 0){
seconds--;
timerLabel.setText(String.format("%02d : %02d : %02d",hours,minutes,seconds));
}
else{
if(minutes != 0 && hours == 0){
minutes--;
seconds = 60;
seconds--;
}
else if(minutes != 0 && hours != 0 ){
minutes--;
seconds = 60;
seconds--;
}
else if(minutes == 0 && hours != 0){
minutes = 60;
minutes--;
seconds = 60;
seconds--;
hours--;
}
timerLabel.setText(String.format("%02d : %02d : %02d",hours,minutes,seconds));
}
}
}
});
addButton = new JButton("Add");
addButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
new TimerSpinner();
}
});
//creates a new timer that runs in separate thread.
newButton = new JButton("New...");
newButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
ExecutorService executor = Executors.newSingleThreadExecutor();
executor.execute(new TimerClass());
executor.shutdown();
}
});
panel1.add(timerLabel);
panel1.add(addButton);
panel1.add(newButton);
startButton = new JButton("Start");
stopButton = new JButton("Stop");
resetButton = new JButton("Reset");
startButton.setEnabled(false);
stopButton.setEnabled(false);
resetButton.setEnabled(false);
panel2.add(startButton);
panel2.add(stopButton);
panel2.add(resetButton);
startButton.addActionListener(new ButtonHandler());
stopButton.addActionListener(new ButtonHandler());
resetButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
timerLabel.setText(String.format("%02d : %02d : %02d",initHr,initMin,initSec));
hours = initHr;
minutes = initMin;
seconds = initSec;
secTimer.restart();
}
});
add(panel1,BorderLayout.NORTH);
add(panel2,BorderLayout.SOUTH);
}
class ButtonHandler implements ActionListener{
@Override
public void actionPerformed(ActionEvent e) {
if(e.getSource().equals(startButton)){
started = true;
secTimer.start();
addButton.setEnabled(false);
}
else if(e.getSource().equals(stopButton)){
started = false;
secTimer.stop();
addButton.setEnabled(true);
}
}
}
class TimerSpinner extends JFrame{
private GridBagLayout gbl;
private GridBagConstraints gbc;
public TimerSpinner(){
super("Enter the Timer values");
gbl = new GridBagLayout();
gbc = new GridBagConstraints();
Insets insets = new Insets(2,5,2,5);
setLayout(gbl);
JLabel message = new JLabel("Start counting from..");
JLabel hoursLabel = new JLabel("Hours");
final JSpinner hourSpinner = new JSpinner(new SpinnerNumberModel(0,0,null,1));
JLabel minutesLabel = new JLabel("Minutes");
final JSpinner minutesSpinner = new JSpinner(new SpinnerNumberModel(0,0,59,1));
JLabel secondsLabel = new JLabel("Seconds");
final JSpinner secondsSpinner = new JSpinner(new SpinnerNumberModel(0,0,59,1));
JButton okButton = new JButton("OK");
JButton cancelButton = new JButton("Cancel");
//adding the components;
gbc.fill = GridBagConstraints.BOTH;
gbc.insets = insets;
addComponents(message,0,0,4,1);
addComponents(hoursLabel,1,0,2,1);
addComponents(minutesLabel,1,2,1,1);
addComponents(secondsLabel,1,3,1,1);
gbc.fill = GridBagConstraints.HORIZONTAL;
gbc.weightx = 2;
addComponents(hourSpinner,2,0,2,1);
gbc.weightx = 1;
addComponents(minutesSpinner,2,2,1,1);
addComponents(secondsSpinner,2,3,1,1);
gbc.weightx = 0;
addComponents(okButton,4,4,2,2);
addComponents(cancelButton,4,6,2,2);
setDefaultCloseOperation(DISPOSE_ON_CLOSE);
setSize(350, 150);
setResizable(false);
setLocationRelativeTo(TimerClass.this);
addButton.setEnabled(false);
setVisible(true);
//adding action listeners to the buttons
okButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
hours = (Integer) hourSpinner.getValue();
initHr = hours;
minutes = (Integer) minutesSpinner.getValue();
initMin = minutes;
seconds = (Integer) secondsSpinner.getValue();
initSec = seconds;
timerLabel.setText(String.format("%02d : %02d : %02d", hours, minutes, seconds));
timerLabel.setForeground(originalColor);
startButton.setEnabled(true);
stopButton.setEnabled(true);
resetButton.setEnabled(true);
addButton.setEnabled(true);
TimerSpinner.this.dispose();
}
});
cancelButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
TimerSpinner.this.dispose();
}
});
}
public void addComponents(JComponent component, int row, int column, int width, int height){
gbc.gridx = column;
gbc.gridy = row;
gbc.gridwidth = width;
gbc.gridheight = height;
gbl.setConstraints(component,gbc);
add(component);
}
}
public static void main(String[] args){
JFrame mainFrame = new JFrame("Timer");
TimerClass timerClass = new TimerClass();
mainFrame.setSize(250,100);
mainFrame.add(timerClass);
mainFrame.setLocationRelativeTo(null);
mainFrame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
mainFrame.setVisible(true);
}
public void run(){
JFrame mainFrame = new JFrame("Timer");
TimerClass timerClass = new TimerClass();
mainFrame.setSize(250,100);
mainFrame.add(timerClass);
mainFrame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
mainFrame.setVisible(true);
}
}
Be a part of the DaniWeb community
We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.