3

i was trying to put a Gif image to simule "Loading" in a JOptionPane with no results. I tryied with URL, with a Local image, with an absolute path to image but i am only able to put an image (png, jpg...) inside of it. My last try is this

final ImageIcon icon = new ImageIcon(new URL("http://www.archisevilla.org/wp-content/themes/archisevilla/images/loading.gif"));
        JOptionPane.showMessageDialog(null, "Cerrando sesión...", "About", JOptionPane.INFORMATION_MESSAGE, icon);

The JOptionPane image's placeholder looks empty when i run this code.

So i wonder: is possible to put a GIF in a JOptionPane, or even in a JFrame?

This is the code i'm using

private void cerrarsesion() throws Exception {

        this.dispose();
        String path = "/com/icono/loading.gif";  
        URL url = this.getClass().getResource(path);
        System.out.println(url);
        ImageIcon icon = new ImageIcon(url);
        createTimerClose(10).start();
        JOptionPane.showMessageDialog(null, "Cerrando sesión...", "About", JOptionPane.INFORMATION_MESSAGE, icon);
        new Login().setVisible(true);

}
Kevin Alemán
  • 480
  • 6
  • 14
  • what about this:http://stackoverflow.com/questions/13963392/add-image-to-joptionpane – Jegg Jun 22 '15 at 18:02
  • I can put images, but for some reason i can't put that gif and any other gif's... – Kevin Alemán Jun 22 '15 at 18:30
  • *i was trying to put a Gif image to simule "Loading"* If something is actually being loaded, it is probably blocking the EDT. 1) Don't block the EDT (Event Dispatch Thread). The GUI will 'freeze' when that happens. See [Concurrency in Swing](https://docs.oracle.com/javase/tutorial/uiswing/concurrency/) for details and the fix. 2) For better help sooner, post an [MCVE](http://stackoverflow.com/help/mcve) (Minimal Complete Verifiable Example) or [SSCCE](http://www.sscce.org/) (Short, Self Contained, Correct Example). – Andrew Thompson Jun 22 '15 at 18:30
  • No no, is more like a visual "Loading" i put a gif to simule the program is working on something (but actually it doesn't) is just visual. – Kevin Alemán Jun 22 '15 at 18:46

2 Answers2

5

It is completely possible and your code must work. Currently I'm running your code and I see the following:

enter image description here

I think you have a problem with accessing the URL of the loading Icon. Maybe a problem with DNS or maybe you are behind a proxy with limited access to some URLs.

Try the following URL in your browser:

http://www.archisevilla.org/wp-content/themes/archisevilla/images/loading.gif

If you can see this loading gif icon in your browser, you must see that in the JOPtionPane.

If you see the loading.gif icon in your browser but you cannot see it JOPtionPane, then try the following:

  1. Download it and put it the package which you are writing the above code. for example if you have a class named Test.java, put the loading.gif file in the same package as Test.java exists.

  2. Write the following code in the main method of Test.java:

    ImageIcon icon = new ImageIcon(Test.class.getResource("loading.gif").getFile());
    JOptionPane.showMessageDialog(null, "Cerrando sesión...", "About", JOptionPane.INFORMATION_MESSAGE, icon);
    

Now you should see the animated loading.gif in you JOptionPane dialog. If you see loading.gif now, you should try finding the problem with accessing that URL from your java code. Sometimes antiviruses or firewalls ban the access of applications such as java.exe (your jvm) for outbound or inbound traffics. Maybe your problem is something like this.

But the answer to your question is Yes. It is possible to load and show GIF files in JOptionPanes or JFrames.

Good Luck.

STaefi
  • 4,297
  • 1
  • 25
  • 43
  • this is what i get [link](http://www.subirimagenes.net/i/150622082942385644.png). I can see it in my browser, but i can't see in that JOptionPane, if i try runing this on other class, it works, but not inside the method i have – Kevin Alemán Jun 22 '15 at 18:29
  • I can see you are using a custom LookAndFeel, is that right? Maybe it is the problem. Sometimes custom LookAndFeels do not implement the same behavior as implemented in the standard ones. Try the code without your custom lookAndFeel. Also you didn't answer my question, do you see the loading.gif in you browser with that url? If yes did you try downloading and use it in local with the code I provided above? – STaefi Jun 22 '15 at 18:34
  • Yes, i can see the image in my browser and it moves like gif does, i downloaded it and save it in the same folder but doesn't works. Maybe the LAF is making the problem, when i run this code on a class without custom LAF it works – Kevin Alemán Jun 22 '15 at 18:37
  • I think that is because of the LAF you are using. – STaefi Jun 22 '15 at 18:41
  • I'm using substance 4 to custom LAF, is there any way to make this work? – Kevin Alemán Jun 22 '15 at 18:47
2

Doing this worked for me.

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

public class TestIcon
{
    public static void main(String[] args) throws Exception
    {
        final ImageIcon icon = new ImageIcon(new URL("http://www.archisevilla.org/wp-content/themes/archisevilla/images/loading.gif"));
        JOptionPane.showMessageDialog(null, "Blah blah blah", "About", JOptionPane.INFORMATION_MESSAGE, icon);
    }
}
DrZoo
  • 1,574
  • 1
  • 20
  • 31
  • i tried it, but i'm not sure why for me it only serves when i create a new class and no with the class i have, it gives me a blank place holder – Kevin Alemán Jun 22 '15 at 18:22
  • Show us the entire body of code that you have. The fragment you show is correct, so we need to see more. – DrZoo Jun 22 '15 at 18:28
  • http://www.subirimagenes.net/i/150622084633471141.png There is the piece of code i'm using (The whole code is about 1000 lines) so this is the important part – Kevin Alemán Jun 22 '15 at 18:46
  • Yeah you understood what I mean haha. I can't see that because the work filter is blocking it. I'm running some stability tests so that's why I'm here. You can edit your answer and add the code there. – DrZoo Jun 22 '15 at 18:55
  • Sorry haha, i put the code in the question, its a method that i call when a button is clicked and `createTimerClose(3).start()` is other method i have to close the JOptionPane after some time. – Kevin Alemán Jun 22 '15 at 19:00