Reputation: 187
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
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