title: Can I add an animated gif file to JButton?
body: <p>I'm trying to add a .gif file into a JButton, but seems like it doesn't work - the gif file didn't move, it displayed like a normal Image.</p>
  
  <p>Here are my codes:</p>
  
  <p>+The ImageButton Class:</p>
  
  <pre><code>import java.awt.Dimension;
  import java.awt.image.BufferedImage;
  import java.io.IOException;
  
  import javax.imageio.ImageIO;
  import javax.swing.ImageIcon;
  import javax.swing.JButton;
  
  /**
   * Button which display a png or gif image.
   * */
  
  public class ImageButton extends JButton {
  
  private static final long serialVersionUID = 1L;
  
  private ImageIcon defaultIcon;
  private ImageIcon hoverIcon;
  
  /**
   * Create a normal JButton.
   * */
  
  public ImageButton(){
      super();
  }
  
  /**
   * Create a new ImageButton Object.
   * @param img1 url of normal image of the button
   * @param img2 url hover image of the button
   * @param width button width
   * @param height button height
   * */
  
  public ImageButton(String img1, String img2, int width, int height){
      super();
      BufferedImage defaultIcon = null;
      BufferedImage hoverIcon = null;
      try {
          defaultIcon = ImageIO.read(getClass().getResource(img1));
          hoverIcon = ImageIO.read(getClass().getResource(img2));
      } catch (IOException e) {
          e.printStackTrace();
      }
      this.defaultIcon = new ImageIcon(defaultIcon);
      this.hoverIcon = new ImageIcon(hoverIcon);
      setIcon(this.defaultIcon);
      setPreferredSize(new Dimension(width,height));
      setBorder(null);
      setOpaque(false);
      setContentAreaFilled(false);
      setBorderPainted(false);
  }
  
  /**
   * Hover the button.
   * */
  
  public void hover(){
      setIcon(hoverIcon);
  }
  
  /**
   * Return the button to normal.
   * */
  
  public void down(){
      setIcon(defaultIcon);
  }
  }
  </code></pre>
  
  <p>+The Test Class:
      import java.awt.FlowLayout;</p>
  
  <pre><code>import javax.swing.JFrame;
  
  public class TestGifButton {
  
  public static void main(String[] args) {
      ImageButton button = new ImageButton("level3.gif","level3.gif", 150, 150);
      JFrame frame = new JFrame();
      frame.setLayout(new FlowLayout());
      frame.add(button);
      frame.pack();
      frame.setVisible(true);
  }
  </code></pre>
  
  <p>}</p>
  
  <p>I used a looping .gif file.</p>
  
  <p>So how can I fix it ? thanks a lot :'(</p>