Timer continues ticking when button is clicked instead of restarting

methuselah

enter image description here

I am working on a Quiz app game (screenshot above). Each time the user clicks on the Next button, I want the timer to restart. Unfortunately this does not happen, the timer keeps ticking. I'm not sure why. What can I try to fix this?

final class Gui extends JFrame {
  private Timer timer;
  private JLabel timerLabel;

  private void createWindow() {
    display = new Display();
    setLayout(new BorderLayout(3, 3));
    add(display, BorderLayout.CENTER);

    JPanel timerPanel = new JPanel();
    timerPanel.setBorder(new EmptyBorder(0, 0, 0, 0));
    timerPanel.setBackground(new Color(0x00bcda));
    add(timerPanel, BorderLayout.PAGE_START);

    timerLabel = new JLabel("01:00", SwingConstants.RIGHT);
    timerLabel.setFont(new Font("Arial", Font.BOLD, 20));
    timerLabel.setHorizontalAlignment(JLabel.RIGHT);
    GridBagConstraints c = new GridBagConstraints();
    c.gridx = 1;
    c.fill = GridBagConstraints.HORIZONTAL;
    c.gridy = 0;
    timerLabel.setForeground(Color.black);
    timerPanel.add(timerLabel, c);

    timer = new Timer(1000, new ActionListener() {
      int time = 60;
      @Override
      public void actionPerformed(ActionEvent e) {
        time--;
        timerLabel.setText(format(time / 60) + ":" + format(time % 60));
        if (time == 0) {
          timer = (Timer) e.getSource();
          timer.stop();
        }
      }
    });
  }

  private class ButtonHandler implements ActionListener {
    @Override
    public void actionPerformed(java.awt.event.ActionEvent e) {
      String cmd = e.getActionCommand();
      if (cmd.equals("Quit")) {
        System.exit(0);
      } else if (cmd.equals("Start")) {
        timer.start();
      } else if (cmd.equals("Next")) {
        timer.restart();
      }
      display.repaint();
    }
  }
}
Sergiy Medvynskyy

Restart of timer provides no reinitialization of your time filed.

int time = 60; // here is your problem

To avoid this problem you need to recreate your timer and start it again. For example you can move your timer initialization in a separate method:

final class Gui extends JFrame {
  private Timer timer;
  private JLabel timerLabel;

  private void createWindow() {
    display = new Display();
    setLayout(new BorderLayout(3, 3));
    add(display, BorderLayout.CENTER);

    JPanel timerPanel = new JPanel();
    timerPanel.setBorder(new EmptyBorder(0, 0, 0, 0));
    timerPanel.setBackground(new Color(0x00bcda));
    add(timerPanel, BorderLayout.PAGE_START);

    timerLabel = new JLabel("01:00", SwingConstants.RIGHT);
    timerLabel.setFont(new Font("Arial", Font.BOLD, 20));
    timerLabel.setHorizontalAlignment(JLabel.RIGHT);
    GridBagConstraints c = new GridBagConstraints();
    c.gridx = 1;
    c.fill = GridBagConstraints.HORIZONTAL;
    c.gridy = 0;
    timerLabel.setForeground(Color.black);
    timerPanel.add(timerLabel, c);

    initTimer();
  }

  private void initTimer() {
    timer = new Timer(1000, new ActionListener() {
      int time = 60;
      @Override
      public void actionPerformed(ActionEvent e) {
        time--;
        timerLabel.setText(format(time / 60) + ":" + format(time % 60));
        if (time == 0) {
          timer = (Timer) e.getSource();
          timer.stop();
        }
      }
    });
  }

  private class ButtonHandler implements ActionListener {
    @Override
    public void actionPerformed(java.awt.event.ActionEvent e) {
      String cmd = e.getActionCommand();
      if (cmd.equals("Quit")) {
        System.exit(0);
      } else if (cmd.equals("Start")) {
        timer.start();
      } else if (cmd.equals("Next")) {
        timer.stop();
        initTimer();
        timer.start();
      }
      display.repaint();
    }
  }
}

Another possibility is to move your time field in your Gui class. In this case you need to reset this field on timer restart.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

How to set Timer when button clicked?

How to add a stopwatch timer when a button is clicked?

Countdown timer stops ticking when the phone screen is locked

My JS timer clock is not stopping when clicked twice on start button

Ticking Timer (golang

Restart timer after button is clicked

jQuery Show div when radio button is clicked with id instead of value

page refreshes instead of submitting form when submit button is clicked

Form submits instead of displaying error when button is clicked

Restarting a Timer

C# Timer (TimeSpan) keeps ticking in background (if statements apply when not true)

How to have a timer in main.cpp in qt creator to update the GUI when a button is clicked?

How to stop timer of form1 when form2 button is clicked

Tkinter when a button is clicked?

Button not responding when clicked

Filtering When a Button is clicked

IndexOutOfRangeException when button is clicked

Overlap when a button is clicked

Button Error When Clicked

How can I make my function run when a value is changed instead of when the button is clicked?

Angular 4 Timer up ticking too fast

Do something at irregular intervals while timer is ticking

Minutes/seconds countdown timer not ticking down?

Timer continues after 0

button should get a diffrent color when clicked on but instead the color of all buttons on that page change

How to apply styles and text to only one button when clicked instead of all buttons in Angular 10

timer starts automatically instead of on a button press in javascript

How to change the color of a button when clicked, and when other button is clicked

How to create a button that deletes all field when reset button is clicked. The following code performs action instead of resetting the fields