Q:

Resetting countdown timer not working + JDialog/JOptionPane in Java Swing game

I'm not that good with Java Swing and I'm trying to use a timer start the game with a delay in 3 seconds
But at the same time I want to show a dialog (also the game has to wait 3 seconds so the focus needs to be on the dialog)
So my dialog is as follow: got this sample code
So in my gameplay panel I do this:
public class GamePlayPanel extends JPanel implements ActionListener {
    // attributes
    private JOptionCountDownTimer countDownDialog;
    public GamePlayPanel(MainWindow mainWindow) {
        // initialization attributes
        initLayoutPanel();
        this.timer = new Timer(DELAY, this);
        // Added a delay of 3 seconds so you can prepare to for the game
        this.timer.setInitialDelay(3000);
        resetTime();
    }        

    public void startGame() {
        this.gamePanel.requestFocus();
        this.countDownDialog.startCountDown();
        startTimer(); // this is my game timer to record the game time
    }

    public void restartGame() {
        this.countDownDialog.resetCountDown();
        startTimer();       
        this.gamePanel.requestFocus();
    }
}

It works fine but if I restart the game the count down timer starts at 0 -> 2 seconds.
Also any better ideas on my class JOptionCountDownTimer? I tried to make it extend a JDialog class but I couldn't get it to work.

A:

Try this out, see if it works for you. You can just grab the dialog  class code. All you need to do is pass to it the parent frame, true for modality and seconds you want. You also may want to pretty it up. I'm just providing the functionality
import java.awt.BorderLayout;
import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;
import javax.swing.Timer;
import javax.swing.border.EmptyBorder;

public class CountDownTimer {
    public CountDownTimer() {
        final JFrame frame = new JFrame();
        JButton button = new JButton("Open Dilaog");
        button.addActionListener(new ActionListener(){
            public void actionPerformed(ActionEvent e) {
                new CountDownTimerDialog(frame, true, 5);
            }
        });

        frame.add(button);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.pack();
        frame.setLocationRelativeTo(null);
        frame.setVisible(true);
    }

    private class CountDownTimerDialog extends JDialog {
        private int count;

        public CountDownTimerDialog(JFrame parent, boolean modal, int seconds) {
            super(parent, modal);
            count = seconds;
            final JLabel countLabel = new JLabel(String.valueOf(seconds), JLabel.CENTER);
            countLabel.setFont(new Font("impact", Font.PLAIN, 36));
            JLabel message = new JLabel("Wait to Start Game");
            message.setFont(new Font("verdana", Font.BOLD, 20));

            JPanel wrapper = new JPanel(new BorderLayout());
            wrapper.setBorder(new EmptyBorder(10, 10, 10, 10));
            wrapper.add(countLabel);
            wrapper.add(message, BorderLayout.SOUTH);
            add(wrapper);

            Timer timer = new Timer(1000, new ActionListener(){
                public void actionPerformed(ActionEvent e) {
                    if (count == -1) {
                        dispose();
                    } else {
                        countLabel.setText(String.valueOf(count));
                        count--;
                    }
                }
            });
            timer.setInitialDelay(0);
            timer.start();

            pack();
            setLocationRelativeTo(parent);
            setVisible(true);
        }
    }

    public static void main(String[] args) {
        SwingUtilities.invokeLater(new Runnable(){
            public void run() {
                new CountDownTimer();
            }
        });
    }
}

