Display a loading image in JavaFX and then display another image

Overview

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.

demo.gif ↑ 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.

environment

Description

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

Caution

Since threads are used, exceptions may occur depending on the processing, so Please take appropriate action.

Recommended Posts

Display a loading image in JavaFX and then display another image
JavaFX8 image display
[Ruby] then keyword and case in
Use Jenkins to build inside Docker and then create a Docker image.
[AWS CLI] Switch roles to get a Docker image in another account
[JavaFX] IllegalArgumentException occurs in new Image (path)
Display the background image in the production environment
Display images with exact AutoLayout ratio using image loading library Nuke in Swift
How to display a web page in Java
A memo when fumbling and developing using JavaFX
Display SF Symbols in different sizes and colors
Run JUnit and Spock in a maven project
A simple rock-paper-scissors game with JavaFX and SceneBuilder
Earth display in equatorial coordinate system in JavaFX 3D (1)
[Personal application work memo] How to display a bar graph and a line graph in one graph
[AWS Lambda] Resize the image saved in S3 and save it in another S3 (using Ruby)
How to implement a circular profile image in Rails using CarrierWave and R Magick