Reputation: 33
I'm trying to create an instance of ImageIcon
according to the instructions here (http://docs.oracle.com/javase/tutorial/uiswing/components/icon.html)
/** Returns an ImageIcon, or null if the path was invalid. */
protected ImageIcon createImageIcon(String path,
String description) {
java.net.URL imgURL = getClass().getResource(path);
if (imgURL != null) {
return new ImageIcon(imgURL, description);
} else {
System.err.println("Couldn't find file: " + path);
return null;
}
}
I have the image in the same folder as the java class, but it returns "Couldn't find file: .....". What should I do?
Upvotes: 1
Views: 180
Reputation: 797
Class.getResource() is for accessing stuff via classloader, e.g. things in the same jar as your application.
To access a file from filesystem create URL from file, e.g. new File(path).toURI().toURL();
Upvotes: 1