my dog learned polymorphism*
The moose likes Swing / AWT / SWT and the fly likes loading image Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "loading image" Watch "loading image" New topic

loading image

luc ndabaneze

Joined: May 06, 2003
Posts: 26
hello ,
i am working on my GUI skill and i stumble on a pretty silly problem but well i guess it is not that silly to me, as i am using no IDE , instead a simple dos command to run my program and a notepad to write my classes,and i can't seem to be able to get my code to find my image file and load them can anybody let me know how to set the path?

There was a young man of Cadiz <br />Who inferred that life is what it is , <br />for he early learnt ,<br /> If it were what it weren't ,<br />it could not be that which it is
Dirk Schreckmann

Joined: Dec 10, 2001
Posts: 7023
I think folks would be better able to help you if you were to post the relevant code you're working on.

When posting code, please be sure to surround the code with the [code] and [/code] UBB Tags. This will help to preserve the formatting of the code, thus making it easier to read and understand.

Now, since your solution will like make use of Swing and/or AWT components, I'm moving this to the Swing / AWT / SWT / JFace forum...

[How To Ask Good Questions] [JavaRanch FAQ Wiki] [JavaRanch Radio]
Pat Hays
Ranch Hand

Joined: Aug 20, 2004
Posts: 138
try this:

jLabel1.setIcon(new ImageIcon("C:\\photos\\pic1.jpg"));

Download Java GUI Builder, <a href="" target="_blank" rel="nofollow"></a>
Craig Wood
Ranch Hand

Joined: Jan 14, 2004
Posts: 1535
Let the class loader find the image for you and then have a look at the URL path it comes up with. You can use this to figure out how to write a relative path string that works. The image must be on you class path for the loader to find it. Start with the image in the current directory for ease.
luc ndabaneze

Joined: May 06, 2003
Posts: 26
thanks guys , here is the code ,

protected static ImageIcon createImageIcon(String path,
String description) { imgURL = CelsiusConverter2.class.getResource(path);
if (imgURL != null) {
return new ImageIcon(imgURL, description);
} else {
System.err.println("Couldn't find file: " + path);
return null;

this the method i used to load my images and everytime it is saying " couldn't find the file" and my image file is on the same directory with
it, that is why i think it is my classpath the problem than my code..
Craig Wood
Ranch Hand

Joined: Jan 14, 2004
Posts: 1535
Let's have a look at the path that is being sent to the createImageIcon method. In we find it here

The path is images/convert.gif which says that the convert.gif image is in a folder named images. If you want the class loader to look for the image in the current directory, ie, same directory as your .class files, then you would use a path of convert.gif.
I agree. Here's the link:
subject: loading image
Similar Threads
how to retrieve image in jsp page from a seperate folder.
Doubt in GenericServlet's log().
Problem with AWT image while sending email thru java