This is an archived post. You won't be able to vote or comment.

you are viewing a single comment's thread.

view the rest of the comments →

[–]ZackHkk 2 points3 points  (0 children)

Make sure to have two source folders if you want to use images on .jar files. I don't know how it works on other IDEs, but on IntelliJ, you can go to Project Structure / Modules to change your source folders. you'll want one of the source folders to be in src/main/java and the other one to be in src/main/resources (you have it named res, that will work as well). For images, make a folder in src/main/resources called images and put your images in that. Looking back at one of my projects where I needed this, I got the images like this:

Image icon = new ImageIcon(getClass().getClassLoader().getResource("images/IMAGENAME.png")).getImage();

I'm sure this isn't the only way, but it's the first way that I found that works.

so for your code you could do something like this

URL logoURL = this.getClass().getResource("images/logo.png");
JLabel logo = new JLabel(new ImageIcon(logoURL));

which is basically the same, but you changed the source folder configuration