Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Define a relative path of image in Java FX

I know this has been asked many times and I have searched far and wide for a solution to this probably simple problem. I'm trying to follow the simple javaFX component tutorials on Oracle's website. I can define an image this way:

Image img = new Image("images/portal.png", 50, 50, true, true);

This works when the image is in a folder inside the "src" folder, but I'm trying to get it to find the image when I have the image folder outside of the "src" folder, like this:

project_root/
 |---src/
      |---Main.java
 |---images/
      |---portal.png    

How can I make this work? All I get is errors saying "Invalid URL or resource not found". I've tried to use the absolute path, tried putting ".." infront of it, tried "HS-Graph/images/portal.png" and everything inbetween :( Thank you!

like image 860
Zorobay Avatar asked Dec 12 '14 15:12

Zorobay


1 Answers

I'm going to answer my own question as I actually found a solution to this! My solution is to use the "file:" prefix when specifying a path. So:

Image img = new Image("file:images/portal.png");

Works perfectly when the image file is outside of my src folder!

like image 154
Zorobay Avatar answered Sep 23 '22 10:09

Zorobay