2

I'm programming a GUI in Java (using Swing of course) and while running my application in Eclipse everything is fine and with icons and whatnot, but after exporting my application the icons are gone.

The icons are located under the class-path in the following directory resources/icons.

JButton browseButton = new JButton("Browse", new ImageIcon("resources/icons/browse.png"));
pharaoh
  • 313
  • 1
  • 3
  • 15
  • Did you have a question? BTW - don't access the icon using a `File`, it will not work for a Jar. Use `URL` instead. – Andrew Thompson Jan 29 '12 at 09:07
  • 1
    With such a question, wouldn't it be a good idea to actually post the code which you use to open the icons – Robin Jan 29 '12 at 09:13
  • 1
    @Robin I suspect that if many of the question askers had lots of 'good ideas' like, search Google and the forum, use a debugger, do the tutorial, RTFM.. that there would be a ***lot*** fewer questions at sites like this. ;) – Andrew Thompson Jan 29 '12 at 09:21
  • @AndrewThompson I on the other hand am always hoping that developers would know what kind of information another developer needs in order to answer a question. – Robin Jan 29 '12 at 09:23

1 Answers1

4

Seems like as guided by @kleopatra, this surely is the right way. When you create resources folder, make sure you do it by right clicking your project, then go to source folder and name it as resources/icons and then manually put your images in that folder, and go back to your eclipse and refresh your project. Now you can use these images in your runnable jar also.

Now in order to use your image/icon in your Classes use this

private ImageIcon getImage(String path)
{
    URL url = getClass().getResource(path);
    System.out.println(url);
    if (url != null)
        return (new ImageIcon(url));
    return null;
}

Here your path will be the name of the image prefix it by a forward slash, which is inside your resources/icons folder like "/myIcon.png" I wanted to be sure, before i post my answer, so tested it. Else give me some time, I will put all steps somewhere for you to see.

More info related to the whole thingy can be found on this another answer of mine :

Load ImageIcon Exception

Community
  • 1
  • 1
nIcE cOw
  • 24,468
  • 7
  • 50
  • 143
  • Thanks man, another think I've learned. Your explanation was awesome. – pharaoh Jan 29 '12 at 10:05
  • @pharaoh357 : With Pleasure, glad I helped you in some way :-) Regards – nIcE cOw Jan 29 '12 at 10:08
  • 1
    And don't forget that file names are case sensitive once you access them inside a jar, even if run on an operating system where the filesystem is not case-sensitive. –  Jan 29 '12 at 10:23
  • @a_horse_with_no_name : Too true :-) Regards – nIcE cOw Jan 29 '12 at 10:30
  • @Downvoter : Care to explain, what is wrong with this thingy :-) Regards – nIcE cOw Jan 29 '12 at 12:12
  • 1
    -1 getSystemResource is _not_ the way to load application resources http://docs.oracle.com/javase/7/docs/technotes/guides/lang/resources.html – kleopatra Jan 29 '12 at 12:34
  • 1
    had to do some reading first - which would have been your job after the last downvote on the same incorrect answer ;-) – kleopatra Jan 29 '12 at 12:36
  • @kleopatra : No where in that document it is written that getSystemResource() is not the appropriate way. It clearly states that "Programs access system resources through the ClassLoader methods getSystemResource and getSystemResourceAsStream. The ClassLoader methods search each directory, ZIP file, or JAR file entry in the CLASSPATH for the resource file.", so depending on your requirement you can use either one. There is nothing wrong in this implementation. Regards – nIcE cOw Jan 29 '12 at 12:57
  • you are free to stick to your errors, obviously. As I'm to downvote whenever I see it ;-) – kleopatra Jan 29 '12 at 13:06
  • @kleopatra : Just because someone said it's an error doesn't mean it really is. No document I had read so far that states that this method is inappropriate for such things. This is really not a game of MONOPOLY, where what you think is always right and what others think is always wrong. Better read that again and find something genuine to make your stand. Regards – nIcE cOw Jan 29 '12 at 13:30
  • @kleopatra : Seems like my eyes really cannot see what your magnifier can. I still cannot see the line that states "THAT THIS METHOD OR THE OTHER METHOD HAS AN EDGE OVER THE OTHER METHOD" :-) Regards – nIcE cOw Jan 29 '12 at 13:59
  • I honestly cannot understand why you don't see it, you are a developer with a good brain, showing in most of your posts :-). Hint: what does the bolded word imply in _All class loaders will search for a resource **first** as a system resource_ ... – kleopatra Jan 29 '12 at 14:31
  • @kleopatra : My Apologies, now I found the line, which says something about the starting point of where the ClassLoader starts searching for things. Got it my bad. Forgive me for being rude. Do check my answer is this the right way now to do this. :-) Regards – nIcE cOw Jan 29 '12 at 15:28