1

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:

JavaFX 2.0 + FXML - strange lookup behaviour

JavaFX How to set scene background image

Community
  • 1
  • 1
Joop
  • 3,706
  • 34
  • 55
  • 1
    You need `fx:id`, not `id`. But the style option in FXML should work. Where is `image.jpg` located in relation to the FXML file? You may need to read the documentation on FXML [location resolution](http://docs.oracle.com/javase/8/javafx/api/javafx/fxml/doc-files/introduction_to_fxml.html#location_resolution). – James_D Feb 21 '15 at 19:25
  • You sir, made my day! Thanks a lot. Such a small mistake, such a big difference. Thanks! – Joop Feb 21 '15 at 20:09

0 Answers0