Article View: borland.public.jbuilder.java.swing
Article #337Re: using icons
From: Lori M Olson [TeamB]
Date: Fri, 19 Aug 2005 15:16
Date: Fri, 19 Aug 2005 15:16
51 lines
2849 bytes
2849 bytes
GEORGE wrote:
> I load icons with ImageIcon("icons/filename.png"). This is fine for my app
> because I always run from within JBuilder so I just have to place the icons
> directory under the project directory. I think this will most likely not
> work if the app was being run from a jar file.
>
> What is the most general way to solve this issue? i.e how to load icons in
> a way that is independent of the way the program is run?
>
> TIA
> George
>
>
There are many different ways of dealing with these issues. It would
take hours to run thru all of them. I'll just try to explain the
problem, and then my preferred solution, but keep in mind that there are
other ways...
The problem you have here is that ImageIcon("icons/filename.png") is
using the form of ImageIcon that is going directly to the file system,
and it is using a relative path. So, this works for JBuilder, because
it always runs from the project directory and finds your files w.r.t
that directory.
A more robust way, is to place the images in your *classpath* (add the
files to your project, and make sure JBuilder is configured to copy
those types of files to the classpath), and then access them as
"resources" from the classpath. The Swing Tutorial actually illustrates
this way of loading images:
http://java.sun.com/docs/books/tutorial/uiswing/misc/icon.html
--
Regards,
Lori Olson [TeamB]
------------
Save yourself, and everyone else, some time and search the
newsgroups and the FAQ-O-Matic before posting your next
question.
Google Advanced Newsgroup Search
http://www.google.ca/advanced_group_search
Other Newsgroup Searches:
http://www.borland.com/newsgroups/ngsearch.html
Joi Ellis's FAQ-O-Matic:
http://www.visi.com/~gyles19/fom-serve/cache/1.html
Technical Details
Message-ID:
<4305f7a7$1@newsgroups.borland.com>Path:
arc1-ca.pugleaf.net!g2news1.google.com!news3.google.com!news.glorb.com!meganewsservers.com!feeder2.on.meganewsservers.com!news-out.newsgroups.com!propagator2-sterling.newsfeeds.com!in.nntp.be!caladan.landsraad.net!newsgroups.borland.com!not-for-mailReferences:
<4303c686$1@newsgroups.borland.com>Show Full Headers
Complete Article Headers
X-Google-Thread: 111d45,873b9a753cc77904 X-Google-Attributes: gid111d45,public X-Google-Language: ENGLISH,ASCII-7-bit Date: Fri, 19 Aug 2005 09:16:06 -0600 From: "Lori M Olson [TeamB]" <javadragon@techie.com> User-Agent: Mozilla Thunderbird 1.0.2 (Macintosh/20050317) X-Accept-Language: en-us, en MIME-Version: 1.0 Newsgroups: borland.public.jbuilder.java.swing Subject: Re: using icons References: <4303c686$1@newsgroups.borland.com> In-Reply-To: <4303c686$1@newsgroups.borland.com> Content-Type: text/plain; charset=ISO-8859-1; format=flowed Content-Transfer-Encoding: 7bit NNTP-Posting-Host: 142.179.181.200 Message-ID: <4305f7a7$1@newsgroups.borland.com> X-Trace: newsgroups.borland.com 1124464551 142.179.181.200 (19 Aug 2005 08:15:51 -0700) Lines: 51 Path: arc1-ca.pugleaf.net!g2news1.google.com!news3.google.com!news.glorb.com!meganewsservers.com!feeder2.on.meganewsservers.com!news-out.newsgroups.com!propagator2-sterling.newsfeeds.com!in.nntp.be!caladan.landsraad.net!newsgroups.borland.com!not-for-mail