iMan
iMan

Reputation: 476

JavaFX - Cannot load image from within project

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?

Upvotes: 2

Views: 5345

Answers (2)

IsaacK
IsaacK

Reputation: 1298

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.

Upvotes: 1

user4073072
user4073072

Reputation: 34

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.

Upvotes: 2

Related Questions