Reputation: 75
I'm having problems displaying icons in a JList that I've made. I'm passing a path to the function getResource() and then storing the resource in a URL object. However, every time I call the getResource() function it returns a null, even though the images exist in the directory.
I've tried looking around on stackoverflow and other sites but I can't find a solution to this problem. I even tried storing the images in multiple locations inside my project directory but to no avail. If it helps, I'm using the Eclipse IDE. So I've stored the files in the /src/, /src/resources/, and the project folder.
Here is my code:
import java.awt.*;
import java.io.*;
import java.net.URL;
import java.util.ArrayList;
import javax.swing.*;
@SuppressWarnings("serial")
public class JListRenderer extends DefaultListCellRenderer
{
private static ArrayList<CakeRecipe> cRecipes;
private ImageIcon[] images;
// Index of cake recipe
private int cakeIndex;
JListRenderer(ArrayList<CakeRecipe> recipes)
{
cRecipes = recipes;
// Initializes an array of ImageIcons FileHander.jpgCounter() counts the amount of JPG's in the folder, FileHandler.getPath() gives you the filepath
images = new ImageIcon[FileHandler.jpgCounter(new File(FileHandler.getPath()))];
for(int i = 0, j = images.length; i < j; i++)
images[i] = createImageIcon("/resources/" + cRecipes.get(cakeIndex).getPhoto()); // cRecipes.get().getPhoto() gives you the filename of the village only (e.g.: photo.jpg)
}
protected static ImageIcon createImageIcon(String path)
{
URL imgURL = JListRenderer.class.getResource(path);
if(imgURL != null)
return new ImageIcon(imgURL);
else
{
System.err.println("Could not find image. Certain menu elements may not display properly");
return null;
}
}
...
Should this code work or am I not using the URL or getResource() properly? The images are in the directory, and I'm passing a path identical to the image's path. Example:
"/resources/photo.jpg"
Any help would be greatly appreciated. Thank you.
EDIT: The problem was with Eclipse. It didn't register the resources in the build path. A simple "Refresh fixed the problem. Thanks for all your help, everyone.
Upvotes: 2
Views: 706
Reputation: 11999
It looks like it should work. I've tried running the following, with a test.txt file in the resources package:
package resourcestest;
import java.io.File;
import java.io.FileReader;
import java.net.URL;
public class Test {
public static void main(String[] args) throws Exception {
final URL test = Test.class.getResource("/");
System.out.println(test);
final URL url = Test.class.getResource("/resources/test.txt");
System.out.println(url);
if(url == null) {
System.out.println("URL is null");
} else {
final File file = new File(url.toURI());
final FileReader reader = new FileReader(file);
final char[] buff = new char[20];
final int l = reader.read(buff);
System.out.println(new String(buff, 0, l));
reader.close();
}
}
}
The first two statements in the main method can be of use to find out what path is considered the relative root at runtime.
I've tried this in NetBeans and Eclipse and in both cases it worked. Now, the real question is, what's your project setup and how do you build? Is there a separate folder for sources and class files or are they put together? Do you use an Ant script or Eclipse build methods?
If you've got the files only in a folder and not in a package, then it's not certain that they're gonna be on the compilation path or on the runtime class path. I suspect the latter may be the case. Try to make sure resources
is a package, not a folder. Try creating a jar file from your project, then check its contents to see if your resources were included. Try running from the jar file and see if it makes a difference.
Upvotes: 3