Question

I am creating an image viewing application where I am displaying all images from a selected folder in a jlist. So they will be displayed in a line and then as a user goes through the list the image is displayed on another jlabel in the same window. The problem is that it's working well for small images but for wallpapers and other folders with large images, I get a Java heap exception. What is the best way to solve this?

public void loadImages(File directory) throws IOException, URISyntaxException {


        File[] imageFiles = directory.listFiles();
        model.removeAllElements();
        for (int ii=0; ii<imageFiles.length; ii++) {
            model.addElement(ImageIO.read(imageFiles[ii])); 
        }


    }
Was it helpful?

Solution

One approach:

  1. As you read each image into your ListModel you will need to create a "thumbnail version" of the image. This will minimize the amount of memory needed to display the images in your JList.

  2. When you select an image to display in the label you will then need to reread the image so you can display the image at its original size.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top