2

I want to add a loading animated gif icon to the output window of my NetBeans platform application that I am developing. I managed to add a png icon file. But in this case, the gif icon added is not animating. it stays the same.

private class Loading extends AbstractAction {

        public Loading() {
            //putValue(SMALL_ICON, ImageUtilities.loadImageIcon("org/netbeans/modules/plsql/execution/loading.gif", true));

            putValue(SMALL_ICON, ImageUtilities.loadImage("org/netbeans/modules/plsql/execution/loading.gif",true));
        }

        @Override
        public void actionPerformed(ActionEvent e) {
            System.out.println("Action performed");
        }

    }

This is what I used as the output window.

final InputOutput io = IOProvider.getDefault().getIO("Deploy Dependents", new Action[]{new Loading()});

To the image that is circled here

2 Answers2

1

You need to add the button as the ImageObserver of the Image you loaded with ImageUtilities.loadImage(). It will take care of the animation for you.

Access to the button itself might be hidden by the IOProvider class but if you manage to get a handle on it, just call image.setImageObserver(button) and you'll see the animation running.

Matthieu
  • 2,736
  • 4
  • 57
  • 87
-1

you can try below code, it is loading gif image with animation.

I think you need only this part of the code:

    ClassLoader classLoader = getClass().getClassLoader();
    File file = new File(classLoader.getResource("gif.gif").getFile());
    Image image = Toolkit.getDefaultToolkit().createImage(org.apache.commons.io.IOUtils.toByteArray(new FileInputStream(file)));
    ImageIcon icon = new ImageIcon(image);

Full code example added below:

import java.awt.Image;
import java.awt.Toolkit;
import java.io.File;
import java.io.FileInputStream;

import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.SwingUtilities;

public class AnimationTest extends JFrame {

public static void main(String[] args) {
    SwingUtilities.invokeLater(new Runnable() {
        public void run() {
            AnimationTest test = new AnimationTest();
            test.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            test.setVisible(true);
        }
    });
}

public AnimationTest() {
    super();
    try {
        JLabel label = new JLabel();
        ClassLoader classLoader = getClass().getClassLoader();
        File file = new File(classLoader.getResource("gif.gif").getFile());
        Image image = 
       Toolkit.getDefaultToolkit().createImage(org.apache.commons.io.IOUtils.
        toByteArray(new FileInputStream(file)));
        ImageIcon icon = new ImageIcon(image);
        label.setIcon(icon);
        icon.setImageObserver(label);
        add(label);
        pack();
    } catch (Exception e) {
        e.printStackTrace();
    }
}

}

added same code with jave project to github, you can get full code from there.

loading gif image to java with animation

Additionally, you need to use below apache commons dependency for the sample code

    <dependency>
        <groupId>commons-io</groupId>
        <artifactId>commons-io</artifactId>
        <version>2.6</version>
    </dependency>
Dilanka M
  • 372
  • 1
  • 5
  • 17
  • This is not actually what I am looking for. I want to add a gif animation to the output window tab – Gihan Saranga Siriwardhana Jan 02 '19 at 03:05
  • what you mean by output window tab? can you share the full code sample? anyway if you want to gif animate, you need to load as stream and make it to the imageIcon. – Dilanka M Jan 02 '19 at 03:50
  • 1
    your question is still not clear. I suggest you to provide your code for ImageUtilities.loadImage(..) and IOProvider.getDefault().getIO(..) related methods. because without that it is not possible to try. – Dilanka M Jan 02 '19 at 10:53
  • I know how to add an image. That's not my problem. If you read the question again, my question was that the image I added was not animating even though it was an animated Gif. And I have provided all the necessary codes – Gihan Saranga Siriwardhana Jan 02 '19 at 11:23
  • 1
    ButtonUI didn't support annimated image rather than label; if you try Dilanka's code you will see. that the easyest way is to do what you whant create a label that will be listenning MouseEvent to perform the action – Arnault Le Prévost-Corvellec Jan 03 '19 at 15:29
  • @Arnault, `JButton` *does* support animation, as it implements `ImageObserver`. – Matthieu Jan 06 '19 at 08:31