I use to do the following :
fileIconLocation = "toolbarButtonGraphics/general/Edit16.gif";
//http://java.sun.com/docs/books/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: " + imgURL.toString());
return null;
}
}
Once inside the Jar it can not find the file and always returns null.
Replacing getClass() with getClass().getClassLoader() seems to fix the problem.
protected ImageIcon createImageIcon(String path, String description) {
java.net.URL imgURL = getClass().getClassLoader().getResource(path);
if (imgURL != null) {
return new ImageIcon(imgURL, description);
} else {
System.err.println("Couldn't find file: " + imgURL.toString());
return null;
}
}
No comments:
Post a Comment