1

I am following the turorial below: (using javafx 20.0.1 and open jdk 20.0.1 --> intellij IDE and Maven build tool)

JavaFX Tutorial Part 4: CSS Styling

At the last part of the tutorial I need to set icon to the scene.

I have followed what is written on the instruction but the image deos not show.

Here is my file structure:

Project File Structure

Here is the code:

this.primaryStage.getIcons().add(new Image("file:images/86957_address_book_icon.png"));

I have noticed that, in the constructor before the location of the image (images/86957_address_book_icon.png) the following was written:

file:

I am new to java and javafx in general. Not sure what is this.

Tried without file: and the following error was thrown:

Exception in Application start method
...
...
Caused by: java.lang.IllegalArgumentException: Invalid URL: Invalid URL or resource not found
...
...

I have tried the following location strings:

added / --> "file:/images/86957_address_book_icon.png"

full path --> "file:com/adnan/addressapp/images/86957_address_book_icon.png"

absolute path --> ONLY THEN IT WORKED

But this is not the solution I want or need.

Could you please help?

Adnan Faradhi
  • 161
  • 1
  • 6
  • 5
    CodeMakery tutorials are usually good, but using a hardcoded `file:` URL there is absolute nonsense. (No matter what path you use, it could never work once the application is bundled as a jar file, for example.) You need to load this as an application resource. See https://stackoverflow.com/questions/61531317/how-do-i-determine-the-correct-path-for-fxml-files-css-files-images-and-other – James_D Jun 29 '23 at 01:31
  • 2
    Thank you James_D! I now fully understand how resource lookup works. That is some detailed and easy explanation to grasp. Amazing. – Adnan Faradhi Jun 29 '23 at 10:20

0 Answers0