AruLNadhaN
AruLNadhaN

Reputation: 2826

Can't add Images to JFrame?

I Have a JFrame.I can't add images to it.I don't know how to give the Correct Image path.

  String iPath = "RemoteItServer/Mobile.png";
                JLayeredPane layeredPane = new JLayeredPane();
                layeredPane.setBounds(0, 0, 315, 610);

                JLabel mobileImageLabel = new JLabel(new ImageIcon(iPath));
                mobileImageLabel.setBounds(0, 0, 315, 610);
                layeredPane.add(mobileImageLabel, Integer.valueOf(0));

enter image description here

If iPath = C://Mobile.png the image is Shown.But if i give iPath = "RemoteItServer/Mobile.png" or iPath = "/RemoteItServer/res/images/Mobile.png".It is not showing the Image.

So Help me in the Right Direction :)

Thanks for your Help ...

Upvotes: 0

Views: 214

Answers (3)

Alan
Alan

Reputation: 822

Paths can be tricky. If a relative path, such as the one provided by Joschua, doesn't work, you can piece together an absolute path like so:

String workingDir = System.getProperty("user.dir");
String separator = System.getProperty("file.separator");

Then just concatenate the relative path to the workingDir:

workingDir + separator + "res/images/Mobile.png";

Another way to do this is to use the File class like so:

File file = new File("res/images/Mobile.png");
String path = file.getAbsolutePath();

Hope this helps.

Upvotes: 1

Joschua
Joschua

Reputation: 69

The following path should work:

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

Edit:

And maybe you forgot to add the pane to the Frame.

getContentPane().add(layeredPane);

Upvotes: 1

David MacNeil
David MacNeil

Reputation: 126

What can help a lot is to put the image directly in the project you are working on. This way you will only have to call "Mobile.png" and not have to worry about the path.

Upvotes: 0

Related Questions