views:

302

answers:

2

I've got a non-looping gif that I use as ImageIcon for two JLabels, but not at the same time.

My problem is that when I set the second JLabel's icon to be the gif, the animation has already been played, so it only show the last frame of it.

Do you know a way to get the animation when the gif is set to the second JLabel?

A: 

Ok,

discarded old answer. After some more searching around I found the way to do it.

ImageIcon icon = ..[the animated gif without looping]..
....
label1.setIcon(icon); //animation plays once
....
// now time to remove icon from label1 and add it to label2
label1.setIcon(null);
icon.getImage().flush(); //reset resource used by the image
label2.setIcon(icon);
....

Java Api: Image#flush()

btw. if you leave the line label1.setIcon(null); out it will still repeat the animation of the icon only for label2. label1 stays at the last frame.

jitter
First : Eclipse says : loadImage() is not visible for imageIcon.Second : doesn't work (just show the last frame).
Cheshire
Replaced old answer with new correct answer
jitter
+1  A: 

On the newly created icon try using:

icon.getImage().flush();
camickr
It works ! Thanks a lot.
Cheshire
lol had the same answer ready for some time too. but only hid the save edit button just now after doing some more tests. well whatever
jitter