Image Not Found Using CSS In Java FX 2

Well, this took me awhile. But it was a pain not being able to load images from within my CSS file. The problem lies because the absolute path is different when you test it compared with the result when deployed.

Let us say this is how I want my button to look like:

When you run it in your local development environment, you wonder why the image would not load. Try to call this within your code

and you will see why. In my case, the result was something like E:/folder/build/classes/images/bullet_black.png

Now, for it to work if you call it from within the CSS file, you would have to do it like this:

The problem then is when you deploy it, there is no drive E from inside the JAR file. So what is the solution then?

Do it programmatically. This is how I did it within my Button class

If only we could use the @- keyword to make it look like this:

But we cannot as it is a Java FX limitation and this keyword is ignored.

Related Posts Plugin for WordPress, Blogger...
  1. 3 Responses to “Image Not Found Using CSS In Java FX 2”

  2. thanks, informative article
    F.A.L.T.U – The community

    By innocent sm on Jul 7, 2013

  3. Not working for me, i had this
    -fx-background-image: url(“graphics/background.jpg”);
    and i tryit a lot and nothing.

    By norman on Sep 10, 2013

  4. @norman: perhaps your graphics folder is placed somewhere else? i did not encounter problems when i did it this way

    By blogmeister on Sep 10, 2013

Post a Comment