How to make an animation button

meow-mew used Ask the Experts™
I got 5 gif files, and I could make it paint and repaint
under JApplet as anumation by Timer. But if I need to
put the animation inside a JButton, how to do that?

I know I could put an image inside a JButton
by the following method:

ImageIcon duke = new ImageIcon("T1.gif");
JButton imageBut = new JButton(duke);//then this is a image button

But I don't know how to put sequence image files into a
JButton to make it like an animation button.
This problem has troubled me for 3 days....
I appreciate if there is any suggestion or examples.

Thanks in advance!
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®

I think the easiest way is to use a program to merge the five .gif file into an animated .gif file. Then just use the animated .gif file for the JButton.

Only if you create a extended button which can receive an array of images for example and paint's them in a sequence. This require to implement a custom button class, a custom ButtonModel class and a custom ButtonUI class to respect the MVC pattern of swing components, by extending the default ones provided.
use the same timer that replaces the images on the applet but this time replace the images on the JButton with setIcon(<icon>) method:
then, repaint the imageBut.

your code should look something like this:

java.util.Timer timer = new java.util.Timer(); // the timer

// runnable that changes the icons
final Runnable buttonAnimator = new Runnable() {
      public void run() {
        < call a method that changes the button icon and repaint it >

//the timer task
java.util.TimerTask timerTask = new java.util.TimerTask(){
      public void run() {

note, in this code it's very important to use the buttonAnimator Runnable and SwingUtilities.invokeLater() method since swing objects are not thread same.

The following is a working example of an animated JButton. I use the javax.swing.Timer class to schedule a task to update the JButton every 100ms.

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.util.*;

public class AnimatedButtonDemo extends JFrame implements ActionListener {

  JButton button = new JButton("Button");
  int index = 0;
  String[] imageNames = {"image0.gif", "image1.gif", "image2.gif"};

  public AnimatedButtonDemo() {
    super("Animated Button Demo");
    Container contentPane = getContentPane();
    contentPane.setLayout(new FlowLayout());
    int delay = 100;
    new javax.swing.Timer(delay, this).start();

  public void actionPerformed(ActionEvent actionEvent) {
    if (index >= imageNames.length) {
       index = 0;
    button.setIcon(new ImageIcon(imageNames[index]));

  public static void main(String[] args) {
    (new AnimatedButtonDemo()).setVisible(true);


Thank you sooooooooooooo much!!

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial