2

I am trying to put a ".gif" image on a label. The image loads , but it is static (no animation). Any guesses why? and how to solve it?

Code that I am using:

BufferedImage img1=ImageIO.read(TCPServer.class.getResource("filecopy.gif"));
JLabel filetransferpic = new JLabel(new ImageIcon(img1));

Note: I don't want to use..

JLabel filetransferpic = new JLabel(new ImageIcon("G:\\filecopy.gif")); 

..approach. Because in this approach I will have to give drive path and place the image in drive. I want the image in project folder "src".

Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
user841852
  • 197
  • 3
  • 7
  • 15
  • 3
    Another question that may help you: http://stackoverflow.com/questions/10622303/loading-animated-gif-in-jlabel-weirdness – FThompson Sep 23 '12 at 19:21
  • 1
    See also [Show an animated BG in Swing](http://stackoverflow.com/questions/10836832/show-an-animated-bg-in-swing). – Andrew Thompson Sep 23 '12 at 22:29

1 Answers1

8

In your example, you are using a BufferedImage. Instead, use an ImageIcon, like this:

ImageIcon gifImage = new ImageIcon(new URL("file:/Path/To/file.gif"));
JLabel yourLabel = new JLabel(gifImage);

This should be animated. But remember, the constructor URL(String) throws a MalformedURLException; make sure to catch it.

mattbdean
  • 2,532
  • 5
  • 26
  • 58