2
3

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 でコードからお手軽に背景画像を挿入する

Last updated at Posted at 2016-04-06

概要

JavaFX で背景画像を設定するサンプルは FXML で指定しているものが多いです。それだと固定の画像しか使えないので、コードから差し込む方法を調べてみました。

コード

root の Pane に対し、下記のコードを追加します。

final String image = new File("path/to/images/splash_bg.jpg").toURI().toString();
controller.background.setStyle(
    "-fx-background-image: url('" + image + "'); " +
    "-fx-background-position: center center; " +
    "-fx-background-repeat: stretch;"
);

ちなみに、画像をjarに同梱する場合は、画像を src/main/resources に配置し、下記のように指定することも可能です。

final String image = ProgressDialog.class.getResource("/splash_bg.jpg").toExternalForm();

参考

  1. Setting background image by javafx code (not css)
2
3
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
2
3

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?