简体   繁体   中英

Java GUI not launching after Thread.sleep

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();
}

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM