0
  JLabel two = new JLabel();
   ImageIcon jaina= new ImageIcon("images/jaina.gif");
  two.setBounds(0,0,300,300);
   two.setIcon(jaina);

then i added the Label to my panel, it plays only once

mdl
  • 23
  • 7
  • 1
    Does it repeat if you view it with anything else, such as a browser? – matt Apr 19 '20 at 14:26
  • 3
    Don't use setBounds(…). Swing was designed to be used with [Layout Managers](https://docs.oracle.com/javase/tutorial/uiswing/layout/visual.html). – camickr Apr 19 '20 at 15:03
  • I was just going to mention the setBounds, if you're using that, I suspect there is an issue with your layout, but we would need more to go on. Without the setBounds, this should absolutely work. – matt Apr 19 '20 at 15:04
  • 1) For better help sooner, [edit] to add a [MCVE] or [Short, Self Contained, Correct Example](http://www.sscce.org/). It should only take 20-30 lines of code in this case. 2) One way to get image(s) for an example is to hot link to images seen in [this Q&A](http://stackoverflow.com/q/19209650/418556). E.G. [This answer](https://stackoverflow.com/a/10862262/418556) hot links to an image embedded in [this question](https://stackoverflow.com/q/10861852/418556). **Caveat:** In this case, I suspect it is a GIF that is encoded to loop only once, better to (upload &) link to that GIF specifically. – Andrew Thompson Apr 19 '20 at 15:25
  • yes turns out it only loops once, but how do keep it looping – mdl Apr 19 '20 at 17:05
  • *"how do keep it looping"* Load it into a GIF maker app. and change the 'loop repeats' attribute. This is really beyond (as in, unrelated to) Swing or Java though. BTW - a tip: Add @matt (or whoever, the `@` is important) to *notify* the person of a new comment. – Andrew Thompson Apr 19 '20 at 17:35
  • @AndrewThompson can you recommend any gif maker website? – mdl Apr 19 '20 at 18:14
  • *"can you recommend any gif maker website?"* Nope. Not just because 'recommend a site' questions are unwelcome on SO, but more because I don't know any. – Andrew Thompson Apr 19 '20 at 18:37

1 Answers1

0

If your gif is not set to loop, the correct solution is to modify the gif to have it loop.

Out of curiosity I mocked up this example. What if I want to animate a gif independent of its animation settings.

import javax.swing.JFrame;
import javax.swing.Timer;
import javax.swing.ImageIcon;
import javax.swing.JLabel;
import javax.imageio.ImageIO;
import javax.imageio.ImageReader;
import javax.imageio.stream.FileImageInputStream;
import java.util.List;
import java.util.ArrayList;
import java.io.File;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.awt.EventQueue;

public class GifLabel{    
    public static void startGui( List<ImageIcon> imgs){
        JFrame frame = new JFrame("animated gif");
        JLabel label = new JLabel( );
        frame.add(label);
        label.setIcon(imgs.get(0));
        Timer t = new Timer( 30, new ActionListener(){
              int i = 0;
              @Override
              public void actionPerformed( ActionEvent evt ){
                  label.setIcon ( imgs.get(i) );
                  i = (i+1)%imgs.size();
              }
          });
      frame.pack();
      frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
      frame.setVisible(true);
      t.start();
    }

    public static void main(String[] args) throws Exception{
      ImageReader reader = ImageIO.getImageReadersBySuffix("GIF").next();
      reader.setInput( new FileImageInputStream( new File("images/jaina.gif")));
      int n = reader.getNumImages( true );
      List<ImageIcon> imgs = new ArrayList<>();
      for(int i = 0; i<n; i++){
          imgs.add( new ImageIcon(reader.read(i)) );
      }
      EventQueue.invokeLater( ()->{
          startGui(imgs);
      });
    }
}

This is way more fragile than just making sure the GIF is the correct format. Also way more code, considering the original new ImageIcon("..."); handles everything.

matt
  • 10,892
  • 3
  • 22
  • 34