0

Im trying to make a window pop up when a certain condition is met, but when the condition is met. The window is not opening.I am using Thread.sleep

code:

public void grow() {
    Thread thread = new Thread(() -> {
        try {
            Thread.sleep(this.harvestTime);
            if(water >= waterNeeded && fertelizer >= fertelizerNeeded) {
                this.harvest = true;
                AlertBox.display("CROP ALERT","A SEED HAS FINISHED GROWING");
                Thread.sleep(60000);
                if(harvest == true) {
                    withered = true;
                    harvest = false;
                    AlertBox.display("ALERT", "FAILED TO HARVEST A CROP. IT BECAME WITHERED!");
                }
            }
            else {
                this.withered = true;
            }
        } catch (Exception e) {
        }
    });
    thread.start();
}
Isaac
  • 21
  • 4
  • GUI frameworks (not just Java, others too) usually do not work from threads, you have to submit GUI activity to the GUI thread. – tevemadar Aug 13 '18 at 10:11
  • 1
    Possible duplicate of [Platform.runLater and Task in JavaFX](https://stackoverflow.com/questions/13784333/platform-runlater-and-task-in-javafx) – tevemadar Aug 13 '18 at 10:11

1 Answers1

0

Java FX methods work only from Java FX thread. You cannot and should not use Java FX components / methods from background threads.

In case you need to show a notification from a background thread use:

Platform.runLater(new Runnable() {
    @Override public void run() {
        bar.setProgress(counter / 1000000.0);
    }
});

In this case Runnable will be added to Java FX event queue and will be processed in Java FX thread.

jreznot
  • 2,694
  • 2
  • 35
  • 53