When you start processing something with JavaFX, display the loaded image and This is a sample program that displays another image showing the processing result when the processing is completed.
↑ is a sample that the processing is started by pressing the button, the processing is completed after 3 seconds, and the processing result is successful.
I referred to a program somewhere, but I wrote it because I forgot it and it took me a long time to find it before.
I put the whole code on GitHub.
When you press the button, the loading image (the spinning image) is displayed first.
SampleController.java
// set loading image
this.image.setImage(new Image(getClass().getResourceAsStream("/images/loader.gif")));
After that, let's start some processing (this time, wait 3 seconds).
SampleController.java
Thread t = new Thread(() -> {
try {
// wait 3 seconds
Thread.sleep(3000);
} catch (InterruptedException e) {
e.printStackTrace();
}
...
In addition, write the processing after the processing is completed (display of the image of the blue circle showing the result).
SampleController.java
...
Platform.runLater(() -> {
// after finishing some processes
// set result image
this.image.setImage(new Image(getClass().getResourceAsStream("/images/maru.png ")));
});
});
Since threads are used, exceptions may occur depending on the processing, so Please take appropriate action.
Recommended Posts