3

I am having a problem adding an image to my button in javafx.

The program hierarchy is as in the image:

enter image description here

the marks are for the files working with the button

the fxml to create the button are following

<Button fx:id="previos" cacheShape="false" layoutX="149.0" layoutY="416.0" mnemonicParsing="false" prefHeight="45.0" prefWidth="114.0" />

the css for the button is the following

#previos{
    -fx-background-image: url("IconsAndImages/left-arrow-icon.png")
}

I also tried to add the full path:

#previos{
    -fx-background-image: url("1A_Students_Attendance_System_Simple/IconsAndImages/left-arrow-icon.png")
}

but I got the same error

The error I am getting is the following:

Jan 08, 2022 12:12:13 PM com.sun.javafx.css.StyleManager$ImageCache getCachedImage
WARNING: Error loading image: file:/C:/Users/aenas/eclipse-workspace/1A_Students_Attendance_System_Simple/bin/application/1A_Students_Attendance_System_Simple/IconsAndImages/left-arrow-icon.png

I tried the solution in the 3rd comment on this question, but I got the same error

  • 1
    Try this in your CSS file: `-fx-background-image: url("/IconsAndImages/left-arrow-icon.png")` I added a single forward slash character before `IconsAndImages`. – Abra Jan 08 '22 at 09:39
  • I recommend shifting to a [standard directory layout](https://maven.apache.org/guides/introduction/introduction-to-the-standard-directory-layout.html). I think it is also best to use maven or gradle and import their project files into your IDE. Then follow the [eden resource guide](https://maven.apache.org/guides/introduction/introduction-to-the-standard-directory-layout.html). – jewelsea Jan 08 '22 at 09:55
  • @Abra that solved the error, thank you, unfortunately the images did not appear on the button yet, and I do not know why – Abdelrahman Nassar Jan 08 '22 at 10:01
  • 1
    @jewelsea unfortunately, I did not use maven or gradle before, I will try to learn how to use them, after that I will try your solution, hopefully it will solve my problem – Abdelrahman Nassar Jan 08 '22 at 10:03
  • [set a graphic](https://stackoverflow.com/questions/16340269/styling-a-javafx-2-button-using-fxml-only-how-to-add-an-image-to-a-button), not a background, to put an image on a button. – jewelsea Jan 08 '22 at 19:17
  • @jewelsea unfortunately, I added the following to my button in the FXML file `style="-fx-graphic: url('/IconsAndImages/left-arrow-icon.png')"` but nothing happened (no error but no Image) – Abdelrahman Nassar Jan 08 '22 at 20:31
  • I believe that it should work if you follow the recommendations in comments and linked tutorials. Also see [loading resources in JavaFX](https://stackoverflow.com/questions/61531317/how-do-i-determine-the-correct-path-for-fxml-files-css-files-images-and-other). Check the location of the image in the binary output directory for your build. – jewelsea Jan 08 '22 at 21:34
  • Also see [Loading classes and resources in Java 9](https://stackoverflow.com/a/45173837/1155209), “non-class-file resources in a module are encapsulated by default, and hence cannot be located from outside the module unless their effective package is open”. I don’t know if that is your issue, but, regardless, following the recommendations in that answer about resource file location is good practice IMO. – jewelsea Jan 08 '22 at 21:41
  • 1
    The doc for [getResourceAsStream](https://docs.oracle.com/javase/9/docs/api/java/lang/Module.html#getResourceAsStream-java.lang.String-), explains the last comment more. Again I don’t know if that is your actual issue though. – jewelsea Jan 08 '22 at 21:49
  • 1
    @jewelsea , Thank you for your help, I will read the resources you supplied me with, hopefully I will find the solution for my problem in one of them – Abdelrahman Nassar Jan 09 '22 at 06:36

0 Answers0