2

i'm trying to make a small project in java with no luck if i'm compiling the program with eclipse everything is good, but when i'm i creating the jar file i get a blank window

this is the image i declared for:

public ImageIcon BACKGROUND = new ImageIcon() ;

I have tried to do the following stuff:

1.

new ImageIcon("Images/wood.jpg").getImage());

2.

this.BACKGROUND.setImage(Toolkit.getDefaultToolkit().getImage("Images/wood.jpg"));

3.

this.BACKGROUND = new ImageIcon(getClass().getResource("Images/wood.jpg"));

4.

/** Returns an ImageIcon, or null if the path was invalid. */
protected ImageIcon createImageIcon(String path,
                                           String description) {

    java.net.URL imgURL = getClass().getResource(path);
    if (imgURL != null) {
        return new ImageIcon(imgURL, description);
    } else {
        System.err.println("Couldn't find file: " + path);
        return null;
    }
}

1 & 2 showing the images after compiling and 3 & 4 returns null

another think is that i'm using mac and when i'm working with windows no image is displayed after compiling.

Janub
  • 1,594
  • 14
  • 26

3 Answers3

2

Here's a small working example if that helps:

public class ImageIconApplet extends JApplet {
    public void init() {
        URL url = getClass().getResource("/images/WhiteFang34.jpg");
        ImageIcon icon = new ImageIcon(url);
        JLabel label = new JLabel(icon, JLabel.CENTER);
        add(label);
    }
}

The jar for the applet on that page contains two files:

/com/whitefang34/ImageIconApplet.class
/images/WhiteFang34.jpg

I'm not sure if you're deploying an applet or a desktop swing app, however the code to load images and the packaging requirements are the same either way.

WhiteFang34
  • 70,765
  • 18
  • 106
  • 111
0

I have something...

ImageIcon icon = new ImageIcon(getClass().getResource("/package/image.png"));
JFrame.setIconImage(icon.getImage());

Place this inside your constructor.

myeongkil kim
  • 2,465
  • 4
  • 16
  • 22
-1

Are you sure Images/wood.jpg is present in the .jar file?

I suggest you unzip the jar file and make sure that it's there. Otherwise you'll have to revise your build scripts (or what ever technique you use) that builds the jar.

Related questions:

Community
  • 1
  • 1
aioobe
  • 413,195
  • 112
  • 811
  • 826
  • after extracting the jar file, there was no Images folder in it, any other solution then http://stackoverflow.com/questions/5607266/imageicon-not-displayed-after-creating-jar ?? – Janub Apr 09 '11 at 20:44