Skip to main content

Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Newcomers » Newcomers » How to use class.getResource() to create an ImageIcon(Where to put the image file so getResource() can find it)
How to use class.getResource() to create an ImageIcon [message #1700625] Sun, 05 July 2015 18:31 Go to next message
Cat Phelix is currently offline Cat PhelixFriend
Messages: 1
Registered: September 2014
Junior Member

I am well acquainted with creating and using ImageIcon icons using the ImageIcon constructor
and putting the image file in a folder called Images which is at the same level as the
bin and src folders.

I discovered a demo program, LayeredPaneDemo, that uses class.getResource() to create
an icon and found that in my eclipse version, the icon's image file was not found when
I used the original getResource() call but the icon was created when I used the ImageIcon

I posted on JavaRanch and eventually realized that the image file needed to be with the
.class files, so I moved the Images folder under bin and getResource() works fine and I'm
happy. Smile

However, I have three questions for you.
One poster on JavaRanch told me that it's better to use getResource() rather than the
ImageIcon constructor for distributing an app (I'm not distributing anything but want
to do it all correctly).
Do you agree with that or can I safe keep using the ImageIcon constructor?

Another poster told me he doesn't think it's safe to leave the image file in bin because
it might be lost during the build in eclipse and that there is a way to have eclipse copy
the files to bin during the build which should mean that I can leave the images folder at
the level of bin and src.
Do you agree with that?
If yes, how do I get eclipse to copy the file during the build?

Before I posted on JavaRanch, I put the Images folder at every level of the project's
directory as shown in eclipse (which is why I missed the bin folder until JavaRanch
whacked me upside the head) and getResource() still didn't work.
Re: How to use class.getResource() to create an ImageIcon [message #1700692 is a reply to message #1700625] Mon, 06 July 2015 13:46 Go to previous message
Nitin Dahyabhai is currently offline Nitin DahyabhaiFriend
Messages: 4044
Registered: July 2009
Senior Member

The contents of the Java Source folder are compiled if they're source files, copied otherwise (assuming no filter's been put in place to prevent copying), so your images belong under a source folder.

Nitin Dahyabhai
Eclipse Web Tools Platform
Previous Topic:Publish a survey link
Next Topic:problem when i try to run eclipse
Goto Forum:

Current Time: Wed May 05 21:49:05 GMT 2021

Powered by FUDForum. Page generated in 0.02870 seconds
.:: Contact :: Home ::.

Powered by: FUDforum 3.0.2.
Copyright ©2001-2010 FUDforum Bulletin Board Software

Back to the top