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
..)?