Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Cloud/Virtualization forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

loading image

 
luc ndabaneze
Greenhorn
Posts: 26
  • Mark post as helpful
  • send pies
  • 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?
 
Dirk Schreckmann
Sheriff
Posts: 7023
  • Mark post as helpful
  • send pies
  • 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...
 
Pat Hays
Ranch Hand
Posts: 138
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
try this:

jLabel1.setIcon(new ImageIcon("C:\\photos\\pic1.jpg"));
 
Craig Wood
Ranch Hand
Posts: 1535
  • Mark post as helpful
  • send pies
  • 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
  • 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
  • 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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic