Loading animated GIF in JLabel weirdness
animated-gif, java, jlabel, swing
Solution
You can try loading your GIF file like that:
public class Test extends JPanel {
public Test() {
ImageIcon imageIcon =
new ImageIcon(Test.this.getClass().getResource("starzoom-thumb.gif"));
}
}
Or using `Test.class.getResouce()` if your context is static.
Problem
I'm trying to load an animated GIF in a JLabel. While this works: ``` URL urlsd; try { urlsd = new URL("http://pscode.org/media/starzoom-thumb.gif"); ImageIcon imageIcon = new ImageIcon(urlsd); JLabel progress = new JLabel(imageIcon); progress.setBounds(5, 20, 66, 66); contentPane.add(progress); } catch (MalformedURLException e) { e.printStackTrace(); } ``` This, on the other hand, does not, and I don't want to get the GIF from an URL, since I already have the GIF. Result of loading this shows only the first frame of the GIF: ``` try { ImageIcon imageIcon = new ImageIcon(ImageIO.read(ClassLoader.getSystemResourceAsStream("res/images/progress_indicator.gif"))); JLabel progress = new JLabel(imageIcon); imageIcon.setImageObserver(progress); progress.setBounds(5, 20, 66, 66); contentPane.add(progress); } catch (MalformedURLException e) { e.printStackTrace(); } ``` I guess there must be a reason for this, but I cannot find it. Thanks! Alex