• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Tim Cooke
  • Campbell Ritchie
  • paul wheaton
  • Jeanne Boyarsky
  • Ron McLeod
Sheriffs:
  • Paul Clapham
  • Devaka Cooray
Saloon Keepers:
  • Tim Holloway
  • Carey Brown
  • Piet Souris
Bartenders:

loading image

 
Greenhorn
Posts: 26
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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?
 
Sheriff
Posts: 7023
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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...
 
Ranch Hand
Posts: 138
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
try this:

jLabel1.setIcon(new ImageIcon("C:\\photos\\pic1.jpg"));
 
Ranch Hand
Posts: 1535
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Greenhorn
Posts: 26
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
thanks guys , here is the code ,

protected static ImageIcon createImageIcon(String path,
String description) {
java.net.URL 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
Posts: 1535
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Let's have a look at the path that is being sent to the createImageIcon method. In CelsiusConverter2.java 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.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic