2

I'm trying to figure out how to add a lot of images into my Java program without having to add every file name in the code.

So I want to take all the .png images in the correct folder and make them into an IconImage and then place them in an ArrayList.

What I've got right now is this.

private void fillList()
{
    File[] files = new File("C:/Users/marre/Google Drive/Java/lek/imageplay/src/img").listFiles();

    for (File f : files)
    {
        if (f.isFile()) 
        {
                ImageIcon tempIcon = new ImageIcon(getClass().getResource("/img/"+f.getName()));       
                List.add(tempIcon);
        }
    }
}    

This works the way I want it to regarding getting the names of all images. But the file only reads from absolute file path.

What can I use instead to get the same result, but that works within .jar file (so it reads from src/img/ and not the whole c:/ bla bla..)?

Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
  • There is no easy way to do that. See http://stackoverflow.com/questions/1456930/how-do-i-read-all-classes-from-a-java-package-in-the-classpath. – JB Nizet Dec 27 '12 at 22:27
  • 1
    The best compromise that I've seen is to generate a file that had all the file names of the images in it, load that file (using Class#getResource) and then load the images. You can create a custom build element to build the file when you compile and build the project – MadProgrammer Dec 27 '12 at 23:22

1 Answers1

0

I'm not sure if this will be helpful but I had a similar issue in attempting to obtain files within a jar file. In the case this can be useful to you, my approach was to obtain the URL for the files inside the jar using the ClassLoader:

ClassLoader classLoader = Thread.currentThread().getContextClassLoader();
URL url = classLoader.getResource("src/img/");

Check inside your jar to make sure of the directory structure for the images so you can specify the correct relative path.

Keep in mind, a jar is a packaged form, so this will most likely return a zipped file (and not a simple File object). You might have to dabble in ZipFile.

ZipFile folderInsideJar = new ZipFile(new File(url.getPath());
Enumeration<? extends ZipEntry> entries = folderInsideJar.entries();
while (entries.hasMoreElements()) {
     // ideally this would be your image file, zipped
    ZipEntry childFile = entries.nextElement();

    // ... etc ...
}

For environments such as JBoss, etc, you might also get virtual files (vfsfile/vfszip). Point being, you can determine what you are dealing with by looking at the result URL.getProtocol().

It would also appear ImageIcon has a constructor that takes a URL. You might try giving this a shot to see if it handles the "resource inside jar" case before manually attempting to retrieve the zip file.

cklab
  • 3,761
  • 20
  • 29
  • An `ImageIcon` is typically used in a Swing app. Swing apps. are regularly deployed using [JWS](http://stackoverflow.com/tags/java-web-start/info) or as applets (or both). The technique described above will fail for both JWS & applets (even if they are trusted). – Andrew Thompson Dec 28 '12 at 03:11
  • Can you recomend anotherway to achive the same effect? getting all pictuers in the folder inside the program in a format i can work with swing? – user1845743 Dec 28 '12 at 10:48