Is there any way to display an animated GIF image in a JLabel like a JPEG or PNG image? I would like to load an animated GIF from a URL to display it in a label.
If I try it with the common method of static images I just receive the first frame of the GIF...
url = new URL("http://example.gif");
image = ImageIO.read(url);
ImageIcon icon = new ImageIcon(image);
picture = new JLabel();
picture.setIcon(icon);
Instead use:
See also Show an animated BG in Swing for why that change works.
In a nutshell, loading an animated GIF using
ImageIOwill create a static GIF (consisting of the first frame of the animation). But if we pass theURLto theImageIcon, it will correctly load all frames of the animation and then run them.So change this:
To this:
Or even this: