I'm new to javafx and I am trying to set the background of a GridPane to an image(, or behind the GridPane). Setting the image in the fxml-file itself did not work. I tried to do this with the JavaFx Scene builder provided by oracle first and then later by manually adding this to the code with: <GridPane style="-fx-background-image: url('board.jpg' ); ...
, but this resulted to nothing being changed when running the application.
I then resorted to setting the background image in the main-method and not the fxml-file. When I try to access the GridPane object (with getNamespace().get()) I get null back, so my problem here is that I can't seem to access the object to then set it's background to the image I want.
This what I tried most recently:
sample.fxml
<GridPane id="pain"...
main.java
FXMLLoader fxmlloader = new FXMLLoader(getClass().getResource("sample.fxml"));
Parent root = fxmlloader.load();
GridPane pane = (GridPane)fxmlloader.getNamespace().get("pain");
pane.setStyle("-fx-background-image: url('image.jpg');");
I already tried to find how to do this without success here: