0

Within my application, I am creating a timerLabel and timerPanel as follows:

// Add timer panel
JPanel timerPanel = new JPanel();
timerPanel.setBorder(new EmptyBorder(0, 0, 0, 0));
timerPanel.setBackground(new Color(0x757575));
c.gridx = 0;
c.fill = GridBagConstraints.HORIZONTAL;
c.insets = new Insets(0,0,0,20);
c.gridy = 0;
centerPanel.add(timerPanel, c);

// Add timer label
JLabel timerLabel = new JLabel("00:00", SwingConstants.RIGHT);
timerLabel.setForeground(Color.black);
timerLabel.setFont(new Font("Serif", Font.BOLD, 30));
timerLabel.setHorizontalAlignment(JLabel.RIGHT);
c.gridx = 1;
c.fill = GridBagConstraints.HORIZONTAL;
c.gridy = 0;
timerLabel.setForeground(Color.white);
timerPanel.add(timerLabel, c);

I would like for the timer to start counting down from 60 seconds whenever the user clicks on the Start, and restarted the button. Below is the ActionListener I have implemented. Right now I am unsure about how to do this:

  private class ButtonHandler implements ActionListener {
    @Override
    public void actionPerformed(java.awt.event.ActionEvent e) {

      // The text from the button that the user clicked.
      String cmd = e.getActionCommand();

      // Respond to Quit button by ending the program.
      if (cmd.equals(GuiText.START.toString())) {
        startGame();
        // start the timer here
      } else if (cmd.equals(GuiText.PREVIOUS.toString())) {
        QuestionList.selectRandomQuestion();
        questionLabel.setText(message);
        // reset the timer here
      } else if (cmd.equals(GuiText.NEXT.toString())) {
        QuestionList.selectRandomQuestion();
        questionLabel.setText(message);
        // reset the timer here
      } else {
        textArea.setText(cmd);
      }
      // Causes the display to be redrawn, to show any changes made.
      display.repaint();
    }
  }

Lastly I need a way of tracking when the timer gets to zero and "doing something" when it does beign able to call a method? How can I accomplish this? My full code is available here (for context): https://pastebin.com/44XWxTQt. I appreciate your time and help.

methuselah
  • 12,766
  • 47
  • 165
  • 315
  • Code for class `QuestionList` is missing. – Abra Oct 27 '19 at 18:35
  • 1
    See: https://stackoverflow.com/questions/7816585/program-freezes-during-thread-sleep-and-with-timer/7816604#7816604. The answer shows your 1) how to use a Swing Timer and 2) how to post a proper [mre]. Once again we are NOT interested in your application. Only the code that demonstrates the described problem and we should be able to compile and test the code. How did you get all those rep points without knowing how to create a proper "MRE"? – camickr Oct 27 '19 at 18:51

1 Answers1

1

Anything involving updating Swing components should revolve around javax.swing.Timer. This has start() / stop() methods to call on your button-clicks. It is also configurable via the constructor to call its own actionPerformed method every second (so you can update the JLabel and repaint).

https://docs.oracle.com/javase/tutorial/uiswing/misc/timer.html

drekbour
  • 2,895
  • 18
  • 28