user841852
user841852

Reputation: 187

JLabel not showing .gif animation

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".

Upvotes: 2

Views: 6842

Answers (1)

mattbdean
mattbdean

Reputation: 2552

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.

Upvotes: 8

Related Questions