0

Basically I want to use the icon.png from the utils/images and not the icon.png fromm the src/app. They are the same but to keep some project order I want to use the utils folder.

enter image description here

Current code: stage.getIcons().add(new Image(getClass().getResourceAsStream("icon.png"))); (This is JavaFX code but my problem is based on Java in general)

I heard you can go folders up by using "../icon.png" I tried that out by putting the icon.png into my src folder (instead of app) and it DID work. But "../" doesn't work twice: "../../icon.png" will not work (so I can't put my image into the main project folder for instance).

It also works if I try "../images/icon.png" (images is a folder I just created [not on the screenshot] and I put it into the src folder but not into the app folder like this:

enter image description here)

But my problem is that I need to go up 2 folder not just 1 to go back down 2 folders.

Going down is no problem but how do I get up 2 when "../../icon.png" won't work?

Edit: Actually,

enter image description here

seems to be the correct folder where all the compiled classes are. So this is the start position of my journey to the utils folder (still in the main project folder). My problem is still the same, just to say I now have to go up (or back) 4 folder instead of 2. Any ideas?

Im using IntelliJ, if this helps.

Edit 2: I found out how to go folders up:

    File projectFolder = new File(getClass().getResource("").toURI()).getParentFile().getParentFile().getParentFile().getParentFile();

But how to go down the 2 now? And also: Will this line of code stop working when I for instance change my IDE because the .class files in IntelliJ are stored in this "out/..." folder construction but in eclipse it looks different and in NetBeans probably too.

Would it be best to just let the icon.png be in the same folder where the executed classes are to prevent what I just said? ...

Thank you!

James_D
  • 201,275
  • 16
  • 291
  • 322
  • Nothing using `File`, and no paths using `..`, will work if and when you bundle your application as a jar file. You're confusing "resources" with "files". Have a look at https://stackoverflow.com/questions/34755630/javafx-location-is-not-set-error – James_D Mar 09 '18 at 20:08
  • What else is in the `out/production/PlayfairFX` hierarchy? Is `utils/images` actually under there, or is it just `images`? – James_D Mar 09 '18 at 20:27

1 Answers1

1

My favored solution (these days) for this is to specifically define a class in a convenient location to find your resources.

E.g. I would just create an interface in images:

images/Images.java

package images ;
public interface Images { }

Then anywhere you need in your application, you can do:

new Image(Images.class.getResource("icon.png"))

(all you need is to import images.Images ; in the class where you are using this).

James_D
  • 201,275
  • 16
  • 291
  • 322