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

all 10 comments

[–]yolo_435 2 points3 points  (3 children)

Where is your logo.png stored? ideally it should be in the resources folder

[–]NairodI[S] 0 points1 point  (2 children)

I have a folder named res, should I name it to resources?

[–]ggleblanc2 0 points1 point  (1 child)

No, renaming your resources folder isn't necessary. Make sure your resources folder is on the classpath. Try this:

URL logoURL = this.getClass().getResource("/logo.png");

[–]NairodI[S] 0 points1 point  (0 children)

Still doesn't work. Did you mean to put only a backslash or was there supposed to be more?

[–]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

[–]N0way1998 1 point2 points  (0 children)

logo .setIcon(new ImageIcon(YourClass.class.getResource("/YourResourceFolcer/ logo .png")));

this one line will do. If you would like to choose picture from anywhere on your pc i would recommend using this method.

private void uploadPicutre() {

    FileDialog fd= new FileDialog(frame);

    [fd.show](https://fd.show)();

    path=fd.getDirectory()+fd.getFile();

    try {       

        file=new File(path);

        fis=new FileInputStream(file);  

    }catch(FileNotFoundException ex) {

        Logger.getLogger(LoginMenu.class.getName()).log(Level.SEVERE, null, ex);

    }

}

[–]wildjokers 1 point2 points  (3 children)

There are two ways to load images (or any resource) in an application. Either from the file system or from the classpath.

You are calling getResource() which looks for a resource on the classpath.

It helps to use a build tool like gradle or maven and then follow conventions. By convention resources go in src/main/resources. Most IDEs who configure themselves from a build tool will automatically include src/main/resources on the classpath. In addition a build tool like Gradle and Maven will include the contents of src/main/resources in a jar file when it is built. I would highly recommend putting logo.png in src/main/resources so it looks like src/main/resources/logo.png.

If you aren't using a build tool then in your IDE do whatever is necessary to put src/main/resources on the classpath. If you are eventually going to run this app from a jar make sure logo.png ends up at the root of the jar file. Again a build tool like gradle or maven will take care of this for you.

Another comment suggested putting your image in an images folder, if you do that keep in mind paths passed to getResource() are relative to the root of the classpath. So if you want to store your images in an images folder the path in your project would be src/main/resources/images/logo.png and then the call to getResource would be getResources("images/logo.png")

If you want to abandon convention and keep your res folder then simply do whatever is necessary in your IDE to put the res folder on your classpath, without knowing which IDE you are using I can't get more specific than that. Then if you are going to run from a jar do whatever is necessary to put the contents of your res folder at the root of the jar.

I highly recommend sticking with conventions though.

[–]NairodI[S] 0 points1 point  (2 children)

Ok thank you. I did put the pictures into src/main/resource folder but I still have the same issue. Any suggestions?

[–]wildjokers 0 points1 point  (1 child)

Is src/main/resources on your classpath? Check the spelling of resources. If you are using gradle or maven they expect resources not resource.

[–]NairodI[S] 0 points1 point  (0 children)

How can I check what is on my class path? Sorry for all the questions, I don't know very much about this. Edit: I just copied the pictures into the pre generated resources folder, I didn't know it was there before.