0
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

JavaFXでローディング画像を表示したあと別の画像を表示する

Posted at

概要

JavaFXでなにか処理を始めたらロードしている画像を表示し、
処理が完了したら処理結果を表す別の画像を表示するサンプルプログラムです。

demo.gif
↑はボタンを押して処理を始め、3秒後に処理が終わり、処理結果が成功だったというサンプルです。

どこかのプログラムを参考したのですが、それを忘れてしまったのと以前探すのに結構時間がかかったため書いておきます。

コード全体はGitHubに置きました。

環境

  • Windows10
  • Java10(Java8でも動くはず)

説明

ボタンを押すとまずローディング画像(くるくる回っている画像)が表示させます。

SampleController.java
// set loading image
this.image.setImage(new Image(getClass().getResourceAsStream("/images/loader.gif")));

その後、何かしらの処理(今回は3秒待つ処理)を始めさせます。

SampleController.java
Thread t = new Thread(() -> {
    try {
        // wait 3 seconds
	      Thread.sleep(3000);
	} catch (InterruptedException e) {
		e.printStackTrace();
	}
...

さらに処理が終わったあとの処理(結果を表す青丸の画像の表示)を書きます。

SampleController.java
...
Platform.runLater(() -> {
	// after finishing some processes
	// set result image
	this.image.setImage(new Image(getClass().getResourceAsStream("/images/maru.png")));
	});
});

注意

スレッドを利用しているため、処理によっては例外が発生することがあるので、
適宜対処してください。

0
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?