2

I'm trying to display a Gif on a simple JFrame. I created the "LoadingGif" class, but I got this error message java.io.IOException : Stream closed a second after the Gif appeared, then it stops.

I call this class with LoadingGif.getInstance.runUI() and the source code of the class is :

public class LoadingGif {

    private static LoadingGif instance = null;

    private JFrame f;
    private JLabel label;
    private URL url;
    private ImageIcon imageIcon;

    private LoadingGif()
    {
         url = TraceaReq.class.getResource("/load.gif");
         imageIcon = new ImageIcon(url);
         label = new JLabel(imageIcon);
    }

    public void runUI()
    {
         f = new JFrame(RQTFGenDOORS.VERSION+" - Loading...");
         f.getContentPane().add(label);
         f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
         f.pack();
         f.setLocationRelativeTo(null);
         f.setResizable(false);
         f.setVisible(true);
         f.setAlwaysOnTop(true);
    }

    public void stopLoading(){
        if(f.isDisplayable())
        {
            f.dispose();
        }
    }

    public static LoadingGif getInstance()
    {
        if(instance == null)
        {
            instance = new LoadingGif();
        }
        return instance;
    }
}

Does anyone know why I got this stream closed ?

Thanks in advance !

Jooooris
  • 21
  • 2
  • 1
    How are you using/calling this class? – Dan W Jun 01 '17 at 14:07
  • 1
    `imageIcon = new ImageIcon(url); label = new JLabel(imageIcon);` The label/icon will load the image asynchronously (other things can happen while it is loading), while this code `if(f.isDisplayable()) { f.dispose();` will cause the JVM to stop loading the image as soon as the frame is displayable. I suspect that is the problem. 1) For better help sooner, post a [MCVE] or [Short, Self Contained, Correct Example](http://www.sscce.org/). 2) One way to get image(s) for an example is to hot link to images seen in [this Q&A](http://stackoverflow.com/q/19209650/418556). – Andrew Thompson Jun 01 '17 at 14:16
  • This class is a singleton. I call this class at the very beginning of my main() by using `LoadingGif.getInstance.runUI()`, then at the very end I call `LoadingGif.getInstance.stopLoading()` The thing is the image is well loaded, and the gif is animated for maybe 0.5s, then I got the error message. – Jooooris Jun 01 '17 at 14:49

1 Answers1

0

I've tried to simplify your sample code firstly. For example, I've just used hard-coded path to the GIF file (it's hard-coded for now to ensure that everything is OK with image location and there is not any issues with classpath, etc). Also I've used constructor of LoadingGif class instead of singleton factory.

My sample code looks like this:

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

public class LoadingGif {

    private JFrame frame;
    private JLabel label;
    private ImageIcon imageIcon;

    private LoadingGif() {
        imageIcon = new ImageIcon("/home/me/Temp/loading-gif/src/animated-penguin-gif-5.gif");
        label = new JLabel(imageIcon);
    }

    public void runUI() {
        frame = new JFrame("MyGif - Loading...");
        frame.getContentPane().add(label);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.pack();
        frame.setLocationRelativeTo(null);
        frame.setResizable(false);
        frame.setVisible(true);
        frame.setAlwaysOnTop(true);
    }

    public static void main(String args[]) {
        LoadingGif loadingGif = new LoadingGif();
        loadingGif.runUI();
    }   
}

And the GIF is animated and displayed without any errors.

In my case GIF file is not huge. May be in your case it's very big and takes some time to load, as already @Andrew Thompson said. Anyway, you can check whether the image is loaded with aid of javax.swing.ImageIcon#getImageLoadStatus method and then check the returned flag. For java.awt.MediaTracker#COMPLETE Javadoc says:

/**
 * Flag indicating that the downloading of media was completed
 * successfully.
 * @see         java.awt.MediaTracker#statusAll
 * @see         java.awt.MediaTracker#statusID
 */
public static final int COMPLETE = 8;
flaz14
  • 826
  • 1
  • 13
  • 25