4

I have an imageIcon as Button, now i would to animate it when you rollover. I tried to use a animated gif (without loop) on setRolloverIcon(Icon). But when i hover again on the button the gif is not playing again. When i use a looped gif then it plays it from a random frame. I tried using paintComponent to draw a Shape or an image as Button, which works fine, but even when i use setPreferredSize() or setSize() or setMaximumSize() the Button uses its default size, as you can see in the picture (middle button). Im using GroupLayout, might this be the problem?

enter image description here

2
  • animated gif will not work. You need to use multiple images as frame.
    – Ashwani
    Aug 16, 2013 at 10:10
  • i thought it needs sth like a sprite. Well I tried that, but the button uses still its default size, as in the image shown.
    – domizai
    Aug 16, 2013 at 10:58

1 Answer 1

5

Seems to work just fine for me...

enter image description hereenter image description here

I used the following icons...(png and gif)...

enter image description hereenter image description here

import java.awt.BorderLayout;
import java.awt.EventQueue;
import java.awt.GridBagLayout;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;

public class AnimatedButton {

    public static void main(String[] args) {
        new AnimatedButton();
    }

    public AnimatedButton() {
        EventQueue.invokeLater(new Runnable() {
            @Override
            public void run() {
                try {
                    UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
                } catch (ClassNotFoundException ex) {
                } catch (InstantiationException ex) {
                } catch (IllegalAccessException ex) {
                } catch (UnsupportedLookAndFeelException ex) {
                }

                JFrame frame = new JFrame("Test");
                frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                frame.setLayout(new BorderLayout());
                frame.add(new TestPane());
                frame.pack();
                frame.setLocationRelativeTo(null);
                frame.setVisible(true);
            }
        });
    }

    public class TestPane extends JPanel {

        private ImageIcon animatedGif;

        public TestPane() {
            setLayout(new GridBagLayout());
            JButton btn = new JButton(new ImageIcon("WildPony.png"));
            btn.setRolloverEnabled(true);
            animatedGif = new ImageIcon("ajax-loader.gif");
            btn.setRolloverIcon(animatedGif);
            add(btn);

            btn.addMouseListener(new MouseAdapter() {

                @Override
                public void mouseEntered(MouseEvent e) {
                    animatedGif.getImage().flush();
                }

            });
        }    
    }
}

I just realised that you are using a non-looping gif. This means you are going to nee to try and "reset" to start it playing again.

Try using something like icon.getImage().flush();, where icon is your ImageIcon. You're going to have to attach a MouseListener to the button to detect the mouseEnter event and reset the ImageIcon...

6
  • please can you test my code, because it doesn't works, your or my too
    – mKorbel
    Aug 16, 2013 at 11:17
  • @mKorbel Instead of using button.setIcon(null), you could try using icon.getImage().flush(), which will force the JVM to reload the icon and start playing it again... Aug 16, 2013 at 11:22
  • not I tried flush(), tried to override the BasicButtonUI, for MetalLaF won't works, something missing some notifiers
    – mKorbel
    Aug 16, 2013 at 11:26
  • @mKorbel Seems to work okay for (as far I as I can, don't have a single loop gif at hand :P) Aug 16, 2013 at 11:27
  • right for SystemL&F required flush(), but still is possible only on setIcon side, setRollover do nothing in Win8/Java7
    – mKorbel
    Aug 16, 2013 at 11:33

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.