6

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

Matthias Braun
  • 32,039
  • 22
  • 142
  • 171
AlejandroVK
  • 7,373
  • 13
  • 54
  • 77
  • ClassLoader is really not the way to access, `Application Resources`, try this link, hope this might be of some help, [HOW TO LOAD IMAGES INTO YOUR PROJECT](http://stackoverflow.com/questions/9864267/load-icon-image-exception/9866659#9866659) – nIcE cOw May 16 '12 at 16:07
  • Why not ClassLoader, as described in this [Java Doc](http://docs.oracle.com/javase/7/docs/technotes/guides/lang/resources.html), which states that "All class loaders will search for a resource first as a system resource, in a manner analogous to searcing for class files" – nIcE cOw May 16 '12 at 16:12
  • @AlejandroVK : For a working example consult this [thread](http://stackoverflow.com/questions/9721421/show-image-by-click-jbutton/9722205#9722205) – nIcE cOw May 16 '12 at 16:26

3 Answers3

12

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.

Matthias Braun
  • 32,039
  • 22
  • 142
  • 171
Xeon
  • 5,949
  • 5
  • 31
  • 52
  • Ahha, I will go with this answer, `getClass().getResource(...)` is the way to go :-) – nIcE cOw May 16 '12 at 16:41
  • I suspect this is the answer. Strange things happen with the streams returned by `getResourceAsStream` (e.g. sound apps. might fail because the stream is 'not repositionable'). But provide an `URL` and it works fine. – Andrew Thompson May 16 '12 at 20:22
  • Thanks, it worked, altough I had to check this http://stackoverflow.com/questions/8960381/runnable-jars-missing-buttons/9278270#9278270 in order to realize that I had to include a slash first...something not required using the getSystemResourceAsStream approach :D – AlejandroVK May 17 '12 at 07:36
  • Note that if your GIF is at `myProject/src/main/resources/icons/myGif.gif` you should pass `"icons/myGif.gif` to `getResource`. This contrasts with the usage of `getResourceAsStream` where you have to prepend a slash to your file path: `"/icons/myGif.gif`. – Matthias Braun May 03 '18 at 10:21
0

Below code worked for me, it will show the animation instead of first frame of the image.

public class Test extends JPanel {
    public Test() {
        ImageIcon yyyyIcon = new ImageIcon(xxx.class.getClassLoader().getResource("yyyy.gif"));
        connectionLabel.setIcon(yyyy);   
    }
}
jmattheis
  • 10,494
  • 11
  • 46
  • 58
0

So there is also a simpler way of doing it. The following line is how I did it.

this.setContentPane(new JLabel(new ImageIcon("Path To Gif File")));
sosumi
  • 57
  • 1
  • 13