0

I'm trying to show a GIF image inside a JLabel using the following code

Image waitImage = null;
        JLabel l1;
        try {
            waitImage = ImageIO.read(IndexesPanel.class.getResource("/images/error.gif"));
            l1 = new JLabel(new ImageIcon(waitImage));
            l1.setBounds(20, 20, 100, 100);
            waitPanel.add(l1);

        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

The image is shown, but it's not animated. I'm using the following Gif:

enter image description here

Any idea?

mKorbel
  • 109,107
  • 18
  • 130
  • 305
MaTTP
  • 945
  • 2
  • 12
  • 27
  • 1
    See http://stackoverflow.com/questions/7724078/how-do-i-show-a-animated-gif-image-using-a-thread ; after setting up the waitPanel, do the work not on the EDT (event dispatching thread), but in a separate thread. Maybe setting the waitPanel in an `EventQueue.invokeLater(new Runnable() { ... });` – Joop Eggen Mar 12 '14 at 13:56

1 Answers1

8

ImageIO returns BufferedImage

Use new ImageIcon(new URL("path to resource"));

Guess you can use new ImageIcon(IndexesPanel.class.getResource("/images/error.gif"));

StanislavL
  • 56,113
  • 9
  • 64
  • 93