0

I want to add a gif loading image that will display on my panel in Java. This is the code. The first image is a png which is display but the second image is a gif which isn't displaying.

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

public class Welcome extends JFrame{
public static void main(String[]args) {
Welcome welcome = new Welcome();
welcome.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
welcome.setSize(600, 350);
welcome.setVisible(true);
welcome.getContentPane().setBackground(Color.white);


}
private JLabel window1;
private JLabel window2;
private ImageIcon logo;
private ImageIcon Spinner;
public Welcome() {
    super ("HALTON'S PHARMACY");
    setLayout(new FlowLayout());
    window1 = new JLabel("");
    window1 = new JLabel(logo);
    window2 = new JLabel(Spinner);
    window1.setIcon(new ImageIcon("halton_logo.png"));
    window2.setIcon(new ImageIcon("Spinner.gif"));
    add(window1);
    add(window2);
    validate();
}
}
Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
  • Is `Spinner.gif` an animated GIF? **General tip:** 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). E.G. [This answer](https://stackoverflow.com/a/10862262/418556) hot links to an image embedded in [this question](https://stackoverflow.com/q/10861852/418556). *BTW - the code works just fine here, with a PNG and an animated GIF from that linked page of images.* – Andrew Thompson Feb 17 '19 at 13:15

2 Answers2

0

Even if it is not referred in the docs (as far as i looked), if you follow ImageIcon(String filename) constructor you will find the following piece of code.

image = Toolkit.getDefaultToolkit().getImage(filename);
if (image == null) {
     return;
}
this.filename = filename;
this.description = description;
loadImage(image);

So, with some kind of guessing, it is because the file with this specific name cannot be found. If the first is showing, it means first image exists. If the second is not showing, it means second image does not exist.

Long story short: Check if the path/name of the .gif file is accurate.

A simple System.out.println(new File("Spinner.gif").exists()); will probably do the job.

George Z.
  • 6,643
  • 4
  • 27
  • 47
0

I can think of three possible explanations:

  1. You have used the wrong filename. (I notice that you have capitalized the filename in "Spinner.gif". Is that a mistake?)

  2. The file does not exist at the required location when you run the command. (I notice that you are using a relative path. That means that the application requires there to be a file called "Spinner.gif" in the application's current directory at runtime.)

  3. The GIF file is malformed or corrupted.


Loading an application's (notionally) built-in images from the file system is probably a bad idea. It would be better to load them as resources; e.g.

    URL url = this.getClass().getResource("/images/spinner.gif");
    ImageIcon icon = new ImageIcon(url);

You then just need to make sure that the required images are in your application's JAR file or whatever.

Stephen C
  • 698,415
  • 94
  • 811
  • 1,216
  • Thank. I had the image placed on the wrong file. Moved it to another folder and it now works. –  Feb 17 '19 at 13:56