Correct way to set the path of a BackgroundImage in JavaFX CSS


Question

I have an AnchorPane and I want to style it by giving it a BackgroundImage I have in another package : File Structure Screenshot

Here's the CSS I tried:

#body 
{
    -fx-background-image:url("/Images/FlowersPattern_beige_background.jpg");   
}

Using javascript I set the AnchorPane's CSS id to body; that didn't work. I tried to check other questions already asked, but they didn't help. Please can you clarify how, exactly, it should be done?

1
3
5/13/2017 3:59:50 AM

Accepted Answer

If your css file is loaded normally, Replacing:

"/Images/FlowersPattern_beige_background.jpg"

with

"../Images/FlowersPattern_beige_background.jpg"

should work.

The path is relative to the css file's directory, the .. accesses the parent directory (src); /Images refers to the Images directory; finally /FlowersPattern_beige_background.jpg accesses your image.

4
5/13/2017 6:15:06 AM

Licensed under: CC-BY-SA with attribution
Not affiliated with: Stack Overflow
Icon