1

When I try to load a gif animation I am just getting an empty frame. Do you know what is wrong? The file is located within extras/loading.gif:

package an1;

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


public class An1 {

 public static void main(String[] args){

    JFrame frame = new JFrame("Test");

    ImageIcon loading = new ImageIcon("extras/loading.gif");
    frame.add(new JLabel(loading, JLabel.CENTER));

    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.setSize(400, 300);
    frame.setVisible(true);


}
}

I got loading.gif using this website http://spiffygif.com with the default settings. I just changed the name from gif.gif to loading.gif. Here it is:

enter image description here

Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
kit
  • 83
  • 4
  • 16
  • Would you share `loading.gif` ? – rdonuk Apr 05 '16 at 16:56
  • 1
    Is your code working with regular jpg images kept in the same location? – Hirak Apr 05 '16 at 16:59
  • I have modified the question to include the gif. Much appreciate the help. – kit Apr 05 '16 at 17:07
  • 1
    Your code is working. I think the problem is in the other parts of your application. Please share more code. – rdonuk Apr 05 '16 at 17:08
  • that's everything. strange that it isn't working for me. Any ideas why that could be? – kit Apr 05 '16 at 17:26
  • @kit, you still haven't answered Hirak's question. If you can't load an image the problem is with your path. Use ImageIO to load the image. You will get an error message if the image can't be loaded. Also, read the section from the Swing tutorial on [How to Use Icons](http://docs.oracle.com/javase/tutorial/uiswing/components/icon.html) for working examples and better ways to load the image. – camickr Apr 05 '16 at 17:29
  • 1
    Does this have something to do with the fact that you have your class placed in a package? I'm wondering if you try it in the default package (just don't put this in a package at all) if that would work. I don't use packages much, so not exactly sure if that would affect you but it runs fine for me without being in a package. – djs Apr 05 '16 at 18:00
  • Application resources will become embedded resources by the time of deployment, so it is wise to start accessing them as if they were, right now. An [tag:embedded-resource] must be accessed by URL rather than file. See the [info. page for embedded resource](http://stackoverflow.com/tags/embedded-resource/info) for how to form the URL. – Andrew Thompson Apr 05 '16 at 23:01

2 Answers2

1

I'm running Ubuntu (Linux) so your code worked fine for me, which leads me to believe that this may be a file-separator issue with the filename. Are you running Windows? If so, try this:

ImageIcon loading = new ImageIcon("extras\\loading.gif");

The file-separator on Windows is "\" and on MAC/Linux is "/". Since "\" is an escape character in Java, you need to use two of them to actually get this to work.

djs
  • 3,947
  • 3
  • 14
  • 28
  • The separator doesn't matter. Java/Windows will handle it. Using the "/" is easier so you don't have to worry about escaping the "\". – camickr Apr 05 '16 at 17:27
  • Im using a mac. Tried /, //, \ and \\ but still not working. Any other ideas? – kit Apr 05 '16 at 17:30
1

Run this source on your machine and check it works..

import java.net.*;
import javax.swing.*;

public class An1 {

    public static void main(String[] args) throws MalformedURLException {
        JFrame frame = new JFrame("Test");

        ImageIcon loading = new ImageIcon(
                new URL("https://i.stack.imgur.com/8IXqb.gif"));
        frame.add(new JLabel(loading, JLabel.CENTER));

        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setSize(400, 300);
        frame.setVisible(true);
    }
}

N.B. Application resources will become embedded resources by the time of deployment, so it is wise to start accessing them as if they were, right now. An must be accessed by URL rather than file. See the info. page for embedded resource for how to form the URL.

Community
  • 1
  • 1
Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433