Re: ImageIcon not finding my images!
From: Jacob (jacob_at_yahoo.com)
Date: 09/22/04
- Next message: Paul Lutus: "Re: JDialog dispose and application window popping"
- Previous message: Mark Murphy: "Re: ImageIcon not finding my images!"
- In reply to: Kane Bonnette: "Re: ImageIcon not finding my images!"
- Next in thread: Kane Bonnette: "Re: ImageIcon not finding my images!"
- Reply: Kane Bonnette: "Re: ImageIcon not finding my images!"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
Date: Wed, 22 Sep 2004 07:14:42 +0200
Kane Bonnette wrote:
> This turns out to be the issue. Now the question is, what do I need to do to
> get Java to find my file?
> I"ve placed the picture in the same folder as my source code and my .class
> files, before I compile
>
> I also need to use a relative path, since this has to be portable
Icons should be accessed through the classpath as you indicate.
This means they should be located in your _destination_ directory
(along with your .class files) on your development machine, and
be bundled into the .jar if that's how you deploy. Typically you
set Make or Ant or your IDE up so that it _copies_ images (or make
a symbolic link) from your source area to your destination area.
Code to access an image via classpath:
URL url = getClass().getResource ("/com/company/path/icon.gif");
ImageIcon icon = new ImageIcon (url);
JLabel label = new JLabel (icon);
Beware: Don't use backslash in the path. This is *not* a file name
(even if it looks like that). It is a _resource_ and Java chose the
slash as a delimiter.
Also: Don't forget the initial "/". This is a common error. Probably
because "jar -tf" don't report it.
Note: Most often images are accessed from within the
_current_ package (that is at least how you should organize it; don't
have one single /com/company/images/ repository). In this case your
code will be easier to maintain if you pick the path name from the
current package rather then spelling it out explicitly.
Code:
String fileName = "icon.gif";
String packageName = getClass().getPackage().getName();
String resource = "/" + packageName.replace('.','/') + "/" + fileName;
URL url = ... // continue with the above
And even if you do have images elsewhere, access it via its package
as outlined here because hardcoding paths like "/com/company/..." breaks
down if your code undergo obfuscating, and: it is easier to move code
around without breaking it.
If you do choose to pick resource from a central repository, consider
making a dummy Java class (or interface) which can be used as a package
handle:
import com.company.images.Images;
:
String packageName = Images.class.getPackage().getName();
"Images" should be as simple as this:
package com.company.images;
public interface Images{}
- Next message: Paul Lutus: "Re: JDialog dispose and application window popping"
- Previous message: Mark Murphy: "Re: ImageIcon not finding my images!"
- In reply to: Kane Bonnette: "Re: ImageIcon not finding my images!"
- Next in thread: Kane Bonnette: "Re: ImageIcon not finding my images!"
- Reply: Kane Bonnette: "Re: ImageIcon not finding my images!"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
Relevant Pages
|