1

I have a Image Icon in my Jframe.I am using a Image in it.When i export into a Runnable Jar.The Image is Not displayed.

String iPath = "res/images/Mobile.png";
JLayeredPane layeredPane = new JLayeredPane();
 layeredPane.setBounds(0, 0, 315, 610);
 InputStream stream = (InputStream) getClass().getResourceAsStream(iPath);
 JLabel mobileImageLabel;
 mobileImageLabel = new JLabel(new ImageIcon(iPath));

          //mobileImageLabel = new JLabel(new ImageIcon(ImageIO.read(stream)));
       // mobileImageLabel = new JLabel(new ImageIcon(AppFrame.class.getResource(iPath)));

                    mobileImageLabel.setBounds(0, 0, 315, 610);
                    mobileImageLabel.setVisible(true);
                    layeredPane.add(mobileImageLabel, Integer.valueOf(0));

I googled & found the getResourceAsStream method.But it seems to throw NullPointerException.

So Help me in the Right Direction :)

Thanks for your Help ...

Note The Method i Tried has been Commented

CommonsWare
  • 986,068
  • 189
  • 2,389
  • 2,491
AruLNadhaN
  • 2,808
  • 5
  • 24
  • 42
  • I too have the same issue better try getResourceAsStream method on another system. – ashokramcse Jan 17 '14 at 18:21
  • 1
    Open your runnable.jar with a zip-application (f.e. 7-zip) and look if the file is contained in the jar and in the respective directory. Often a wrong setting prevents the resource from being included into the jar. – Roman Vottner Jan 17 '14 at 18:22
  • See [tag:embedded-resource] info. – trashgod Jan 17 '14 at 20:50

2 Answers2

0

To set a image in a button I did this:

JButton yourButton = new JButton("text button");
        try {
            yourButton.setIcon(new ImageIcon(ImageIO.read(getClass().getResourceAsStream("/images/icon-for-your-button.png"))));
        } catch (IOException e3) {
            // TODO Auto-generated catch block
            e3.printStackTrace();
        }

You must have the images inside your project like this:

enter image description here

In your case, the answer I think is to do this:

new ImageIcon(ImageIO.read(getClass().getResourceAsStream("/images/icon-for-your-button.png")))
carexcer
  • 1,407
  • 2
  • 15
  • 27
0

Try to replace

String iPath = "res/images/Mobile.png";

By

String iPath = "C:/..../res/images/Mobile.png";

Whenever you are working with images try to put the complete path for the image. So when you will generate your .jar file and try to run it from different place(may be possible that you want the jar should be run from desktop) you will get all the images.

ravibagul91
  • 20,072
  • 5
  • 36
  • 59