Question

I'm making a timer to go along with a sudoku grid im making and the code in bold bellow is giving me errors and i dont know why. If anyone could point out any mistakes and offer a solution that would be helpful. Thank you.

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;

public class Timer extends JPanel {


    private JLabel timeDisplay;
    private JButton resetButton;
    private JButton startButton;
    private JButton stopButton;

    public Timer(){

        final Timer timer;
        startButton = new JButton("Start Timer");
        stopButton = new JButton("Stop Button");
        timeDisplay = new JLabel("...Waiting...");
        resetButton = new JButton("Reset Timer");

        this.add(resetButton);
        this.add(startButton);
        this.add(stopButton);
        this.add(timeDisplay);

        class TimerClass implements ActionListener{

            int counter;

            public TimerClass(int counter){
                this.counter = counter;
            }

            @Override
            public void actionPerformed(ActionEvent tc) {

                counter++;

            }

        }

        class startButtonaction implements ActionListener{
            public void actionPerformed(ActionEvent e){
                int count = 0;
                timeDisplay.setText("Time Elapsed in Seconds: " + count);

                TimerClass tc = new TimerClass(count);
                **timer = new Timer(1000, tc);
                timer.start();**
            }
        }

    }

}
Was it helpful?

Solution

You're using the TimerClass variable as a local variable in an inner class, and have not declared it as a final variable. You could do that, or declare the variable at the class level.

Note, in the future if you have questions about errors, post the error message.


Edit
Problem 2: You've named this class Timer! This will cause a name clash when you try to use the Swing Timer. Rename your class to something else, say MyTimer.


e.g.,

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;

public class MyTimer extends JPanel {

   private JLabel timeDisplay;
   private JButton resetButton;
   private JButton startButton;
   private JButton stopButton;
   private Timer timer;

   public MyTimer() {

      startButton = new JButton("Start Timer");
      stopButton = new JButton("Stop Button");
      timeDisplay = new JLabel("...Waiting...");
      resetButton = new JButton("Reset Timer");

      this.add(resetButton);
      this.add(startButton);
      this.add(stopButton);
      this.add(timeDisplay);

   }

   private class TimerClass implements ActionListener {

      int counter;

      public TimerClass(int counter) {
         this.counter = counter;
      }

      @Override
      public void actionPerformed(ActionEvent tc) {
         counter++;
      }

   }

   private class startButtonaction implements ActionListener {
      public void actionPerformed(ActionEvent e) {
         int count = 0;
         timeDisplay.setText("Time Elapsed in Seconds: " + count);

         TimerClass tc = new TimerClass(count);
         timer = new Timer(1000, tc);
         timer.start();
      }
   }

}

You've also got problems with your count variable as it cannot be local to the Timer's ActionListener and the Button's as well. It should be a field of the main program.

For a better example of a working timer, please see my answer here.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top