Search code examples
javacssjavafxfxmlscenebuilder

Background Image shows in SceneBuilder but not when App runs


Already searched for an answer for this but the only similar question I could find was here (JavaFX background-image works in scenebuilder and win but not on osx) and it hasn't been answered.

I'm trying to get a background image to show when my App runs. I'm using Eclipse, JavaFX and SceneBuilder. I have applied the CSS File to my FXML file. At the minute the background image will only show in SceneBuilder.

CSS Code:

#background {
-fx-background-position: center;
-fx-background-color: #BDBDBD;
-fx-background-image: url('./Background.png');
-fx-background-repeat: stretch;
}

Anyone got any idea on why this is?

P.S. I have already used two different paths to get to the image, both the image file location within my project and the image file location on my laptop.


Solution

  • you can try this code

    .background {
    -fx-background-position: center;
    -fx-background-color: #BDBDBD;
    -fx-background-image: url('file:src/Background.png');
    -fx-background-repeat: stretch;
    }