Cannot add images, icons to Java programs using Oracle JDeveloper

I just started using JDeveloper for making Java Swing GUI desktop applications and I cannot figure out or find in JDeveloper help that when I want to use images inside jLabels or gif icons for my jFrames, where do I put those image files so that when I build and run the program and lines like the following are executed, the images actually show up:

example:

aboutFrame.setIconImage(new ImageIcon("icons/info.gif").getImage());


So, where do I put my /icons folder with the   info.gif   image file in it?

Is there anything else I need to do, from inside JDeveloper, so that the above line setting an icon image from a gif file works?

I thought with this example just putting the image files into the /icons folder into the /classes folder associated with the JDeveloper project (<project_name>/classes/icons) would do it like with other Java IDE I have used such as JCreator, but it does NOT pull it in with JDeveloper when I run my application.

Why is that? What can I do so that images are shown in my running program?

The program compiles and runs, just no image icons are shown.

Thank you.
abarnaiAsked:
Who is Participating?
 
dileepan_kCommented:
You may place the images relative to classes folder (classes/icons/info.gif) and load them like below

java.net.URL imgURL = getClass().getResource("icons/info.gif); // load from folder OR jar file

aboutFrame.setIconImage(new ImageIcon(imgURL).getImage());
0
 
abarnaiAuthor Commented:
Yes it worked! Thank you.

The original line was in a STATIC method though,  so I got the compiler message that getClass().getResource()  cannot be placed in a static method.

I moved it to the constructor of the class, and then it worked fine.
0
 
abarnaiAuthor Commented:
You made a small typo, not a big deal.
(Left off one quote mark in the code)
0
 
dileepan_kCommented:
Yes. there is a typo. Good to see that it helped
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.