2

My JavaFX project can load image in one machine, but same code cannot load the image in another machine.

I have the package structure (in src) - /com/mypackagestructure/view/images/ and this holds the image file.

I have the CSS file at - /com/mypackagestructure/view/Login.css

Login.css
.root {
     -fx-background-image: url("/com/mypackagestructure/view/images/background.jpg");
     -fx-background-repeat: stretch;
     -fx-background-size: 100%;
     -fx-background-color: transparent;
}

I have this exact same code in another machine (same OS, same Eclipse IDE and JDK/JRE (1.8) installed) and it works over there. But in this machine I get the following message-

Aug 08, 2016 3:36:39 PM com.sun.javafx.css.StyleManager getCachedImage
WARNING: Error loading image: file:/com/mypackagestructure/view/images/background.jpg

In the css if I make the following change, then it works.

-fx-background-image: url("images/background.jpg");

What is wrong with my way of referencing the image file? And why does it work on one machine and not in the other?

iMan
  • 456
  • 1
  • 7
  • 18

2 Answers2

2

i also have experienced the same problem like what you had. these happen sometimes because of build files and you can try tou clean and build it.

basically if you define -fx-background-image: url("images/background.jpg"); then it is direcly refer to your project package location of your images.

if you want to define it with your previous way, then you have to write it down with all complete directory example C:\Users\NILAM\Documents\NetBeansProjects\Aplikasi Arsip Kesbangpol\src\Gambar\image.jpg

hopefully answer your question.

  • Yes, it works when I use the absolute path on drive (C:\\\\. But it doesnt solve the problem as in run time the JAR can be anywhere. For now, only relative path works, absolute path wont work on this machine. And yes, I tried cleaning, building multiple times. – iMan Aug 09 '16 at 06:57
  • if you want to use absolute path, then you have you change the directory with double back slash C:\\Users\\NILAM\\Documents\\NetBeansProjects\\Aplikasi Arsip Kesbangpol\\src\\Gambar\\image.jpg – user4073072 Aug 09 '16 at 08:37
  • I meant the absolute path within the runtime environment, not from disk :). (/com/.../image.jpg) – iMan Aug 09 '16 at 09:49
1

This may be way way too late but I achieved this by having a folder in resources folder called images [Then added my logo.png]. and used the Image in FXML like

<HBox fx:id="myHBox" style="-fx-background-image: url('images/logo.png')" BorderPane.alignment="CENTER" />

This works okay for me.

IsaacK
  • 1,178
  • 1
  • 19
  • 49