-1

I want to show the user their clicks per seconds (CPS), therefore I want to set the Label, which displays the clicks, every second to 0. The problem is that I need to set the Label to static, and this is not working. Here is my code:

 public class Controller{

    public  Button clickButton;
    public  Label cpsLabel;
    public  Label highscoreLabel;
    static int count = 0;
//  static int highscoreInt = 0;
    public void handleButtonClick() {
        count++;
        cpsLabel.setText("CPS: " + count);
    }
    public static void Timer() {
        ScheduledExecutorService exec = Executors.newSingleThreadScheduledExecutor();
        exec.scheduleAtFixedRate(new Runnable() {
          @Override
          public void run() {
              Controller.count = 0;
              cpsLabel.setText("CPS: " + count);
          }
        }, 0, 1, TimeUnit.SECONDS);
        }

}
Michel
  • 33
  • 3

1 Answers1

0

You should not modify the JavaFX components outside the Java FX Main Thread not even put static on the component. Please, check the following article about Concurrency in JavaFX

Snix
  • 543
  • 4
  • 12
  • Could you give me an example for my application, please? – Michel Jun 13 '20 at 22:10
  • there are tons of examples .. just do a bit of research (best after working through a basic tutorial on javafx so that you understand them :) – kleopatra Jun 14 '20 at 04:28