Member Avatar for Steve2490

Ive created a memory game which has a grid of JButtons, when the button is clicked it changes the ImageIcon background to an animation which shows the front of the card.
The problem is that when the button is clicked, the gif doesn't begin playing until the mouse is moved off the button and if the mouse is moved back on before the animation is finished it causes the animation to restart. Has anyone encountered this problem before? Or can think of a workaround.


Recommended Answers

All 4 Replies

Seems like you are working on bydefault class of JBUTTON, you can customize classes in java according to your needs...

AS JBUTTON implements imageobserver class you better check you desired function and overwrite it according to your own needs.

Member Avatar for Steve2490

Thanks, would i need to remove the implementation of image observer for this to work? also whats the best way to go about this?

Just write your own function, it will be overwritten automatically. Search the necessary function which you want to edit.


import javax.swing.*;

class ShowAnimatedGif {

    public static void main(String[] args) throws Exception {
        final URL url = new URL("");
        SwingUtilities.invokeLater(new Runnable() {
            public void run() {
                //JLabel l = new JLabel(new ImageIcon(url));
                //JOptionPane.showMessageDialog(null, l);
                JButton btn = new JButton(new ImageIcon(url));
                JOptionPane.showMessageDialog(null, btn);
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.