Search code examples
javalistswinglistcellrenderer

SetIcon in CellRenderer causing lag


I'm using a custom listcellrenderer with a list and setting both a title and an image but it's really laggy because the image keeps loading everytime i scroll. Is there any way i can fix this ? Here is my code:

 @Override
public Component getListCellRendererComponent(JList<? extends Movie> list, Movie value, int index, boolean isSelected, boolean cellHasFocus) {
    
        this.titleHolder.setText(value.getTitle());
        this.imageHolder.setIcon(new ImageIcon(value.getPoster()));
        
    
    return this;

}

I basically want the image to stop loading everytime i scroll through the list because it's causing the app to lag really heavly. Thanks in advance.


Solution

  • The solution will be to create some sort cache as already mentioned by weisj in comment section.

    Implement the hashCode() and equals() method of Movie class if you have not done already.

    Now create your cache:

    private Map<Movie,ImageIcon> moviePostersCache = new HashMap<>();
    

    Then, inside getListCellRendererComponent:

    @Override
    public Component getListCellRendererComponent(JList<? extends Movie> list, Movie value, int index,
            boolean isSelected, boolean cellHasFocus) {
    
        this.titleHolder.setText(value.getTitle());
        
        
        if (!moviePostersCache.containsKey(value))
            moviePostersCache.put(value, new ImageIcon(value.getPoster()));
        
        this.imageHolder.setIcon(moviePostersCache.get(value));
        return this;
    }
    

    And the posters will be on memory. After first render they will not be "reloaded" - Movie#getPoster will not be called.

    Now, if there a lot of movies that come and go to the gui, consider using a weak hash map.