-1

I'm trying to make a stopwatch in java.

My first problem is that on line 46 I should start timerMethod in a new thread but I couldn't figure out how to do that.

The second problem is that lines 44, 45 and 53 have errors that say "Local variable isTiming defined in an enclosing scope must be final or effectively final".

Code:

import java.awt.Container;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;

public class Stopwatch {
    public static void main(String[] args) {
        windowMethod();
    }
    public static void windowMethod() {
        //create frame
        JFrame frame = new JFrame();
        frame.setSize(900, 600);
        frame.setResizable(false);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setVisible(true);
        //create panel
        JPanel panel = new JPanel();
        frame.add(panel);
        panel.setLayout(null);
        //create time label
        JLabel timeLabel = new JLabel("00:00:00");
        panel.add(timeLabel);
        timeLabel.setFont(new Font("Tahoma",Font.PLAIN, 32));
        Dimension timeLabelSize = timeLabel.getPreferredSize();
        int timeLabelx = (450-(timeLabelSize.width/2));
        timeLabel.setBounds(timeLabelx, 50, timeLabelSize.width, timeLabelSize.height);
        //create start/stop button
        JButton startStopButton = new JButton("Start Timer");
        panel.add(startStopButton);
        startStopButton.setFont(new Font("Tahoma",Font.PLAIN, 32));
        Dimension startStopButtonSize = startStopButton.getPreferredSize();
        int startStopButtonx = (450-(startStopButtonSize.width/2));
        startStopButton.setBounds(startStopButtonx, 150, startStopButtonSize.width, startStopButtonSize.height);
        boolean isTiming = false;
        startStopButton.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                if (isTiming = false) { //This runs if the button is clicked to start the timer
                    isTiming = true;
                    timerMethod(isTiming); //runs the timerMethod in a new thread?
                    startStopButton.setText("Stop Timer");
                    Dimension startStopButtonSize = startStopButton.getPreferredSize();
                    int startStopButtonx = (450-(startStopButtonSize.width/2));
                    startStopButton.setBounds(startStopButtonx, 150, startStopButtonSize.width, startStopButtonSize.height);
                }
                else { //This runs if the button is clicked to stop the timer
                    isTiming = false;
                    startStopButton.setText("Start Timer");
                    Dimension startStopButtonSize = startStopButton.getPreferredSize();
                    int startStopButtonx = (450-(startStopButtonSize.width/2));
                    startStopButton.setBounds(startStopButtonx, 150, startStopButtonSize.width, startStopButtonSize.height);
                }
            }
        });
    }
    public static String timerMethod(boolean isTiming) {
        int milliseconds = 0;
        int seconds = 0;
        int minutes = 0;
        while(isTiming = true) {
            milliseconds++;
            if (milliseconds > 999) {
                milliseconds = 0;
                seconds++;
                if (seconds > 59) {
                    seconds = 0;
                    minutes++;
                }
            }
        }
        String outputTime = (minutes + ":" + seconds + ":" + milliseconds);
        return outputTime;
    }
}
Cœur
  • 37,241
  • 25
  • 195
  • 267
Finn Perry
  • 67
  • 1
  • 7
  • [run on a thread](https://stackoverflow.com/questions/3489543/how-to-call-a-method-with-a-separate-thread-in-java) and [solve the local variable scope issue](https://stackoverflow.com/questions/25894509/problems-with-local-variable-scope-how-to-solve-it) – elirandav Jun 13 '17 at 14:07
  • Possible duplicate of [How to call a method with a separate thread in Java?](https://stackoverflow.com/questions/3489543/how-to-call-a-method-with-a-separate-thread-in-java) – Jaroslaw Pawlak Jun 13 '17 at 14:22

1 Answers1

0

The timerMethod should actually be the run() method of a class extending the Thread class, which should have a way to be stopped from another thread.

May I suggest you to read carefully the Concurrency lessons on the Oracle website https://docs.oracle.com/javase/tutorial/essential/concurrency/index.html?

kekolab
  • 801
  • 10
  • 24