简体   繁体   中英

JavaFX - GridPane Background Image

I've been searching around the internet, and I still haven't found a solution.

All I want to do is: Set an image to my GridPane in a POPUP-window, that I've made, when a button is clicked.

Some programmers refers to:

grid.setStyle("-fx-background-image: url('URL')");

Which were my suggestion as well, but it does not seem to work. I've also tried to set Image image = new Image(); and then set an ImageView, but with no luck!

Hope someone in here has a quick fix for this answer.

//root.setStyle("-fx-background-image: url(' https://encrypted-tbn1.gstatic.com/images?q=tbn:ANd9GcQxsasGQIwQNwjek3F1nSwlfx60g6XpOggnxw5dyQrtCL_0x8IW ')");

This worked out. But not all url's it likes.

In case you are using FXML, you have to add stylesheets to your GridPane in the Controller class. For example, gridPane is the reference variable of your GridPane and app.css is a name of your stylesheets:

gridPane.getStylesheets().addAll(getClass().getResource("/css/app.css").toExternalForm())

Then write something like this in your stylesheets:

#gridPane { -fx-background-image:url("file:C:/path/to/your/project/folder/src/main/resources/image.jpg"); }

In addition, you can add stylesheets app.css to GridPane in the SceneBuilder. The aforementioned procedure worked for me.

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