15

Hello I am writing a GUI application on Java 1.6 with Swing.

I have a pop up screen that should display a gif animation while my Swing gui is loading and also a little bit after.

My pop up screen is a JDialog. Tthe animation should be displayed on a JLabel that was added to the Jdialog the following way:

ImageIcon myImgIcon = getMyImgIcon();
JLabel imageLbl = new JLabel(myImgIcon);
add(imageLbl, BorderLayout.CENTER); 

Now the thing is that the animation only displays after the gui has been loaded. I believe that while the GUI is loading (which is a heavy operation in my application) the EDT is so busy it can't run the animation.

See How do I show a animated GIF image using a thread.

Now the thing is it would be wrong for me to make the GUI load on a different thread (not EDT) so I don't know how to solve the problem.

Does anyone have an idea?

Community
  • 1
  • 1
whomaniac
  • 1,258
  • 4
  • 15
  • 22
  • 5
    Consider using a [splash screen](http://docs.oracle.com/javase/tutorial/uiswing/misc/splashscreen.html) instead if it is for the start-up of your application – Robin Sep 24 '12 at 13:44

4 Answers4

10

You just have to free EDT thread of some heavy tasks and do them in a separate thread. In that case gif animation will work together with other processes running.

You might also create your application interface in a separate thread (yes yes, not inside the EDT) but only until you display it. Afterwards you have should make all changes inside the EDT, otherwise you might encounter a lot of problems.

You can also load more UI elements in a separate thread later, just make sure that you add them onto displayed frames/containers inside EDT - that is the most important thing.

Here is a small example of "heavy-like" interface loading:

public static void main ( String[] args ) throws InvocationTargetException, InterruptedException
{
    // Main window

    final JFrame frame = new JFrame ();

    final JPanel panel = new JPanel ( new FlowLayout ( FlowLayout.LEFT, 5, 5 ) )
    {
        public Dimension getPreferredSize ()
        {
            Dimension ps = super.getPreferredSize ();
            ps.width = 0;
            return ps;
        }
    };
    frame.add ( new JScrollPane ( panel ) );

    frame.setSize ( 600, 500 );
    frame.setDefaultCloseOperation ( JFrame.EXIT_ON_CLOSE );
    frame.setLocationRelativeTo ( null );

    SwingUtilities.invokeAndWait ( new Runnable ()
    {
        public void run ()
        {
            frame.setVisible ( true );
        }
    } );

    // Load dialog

    final JDialog load = new JDialog ( frame );

    JPanel panel2 = new JPanel ( new BorderLayout () );
    panel2.setBorder ( BorderFactory.createEmptyBorder ( 15, 15, 15, 15 ) );
    load.add ( panel2 );

    final JProgressBar progressBar = new JProgressBar ( 0, 100 );
    panel2.add ( progressBar );

    load.setModal ( false );
    load.pack ();
    load.setLocationRelativeTo ( frame );

    SwingUtilities.invokeAndWait ( new Runnable ()
    {
        public void run ()
        {
            load.setVisible ( true );
        }
    } );

    // Heavy task (takes approx. 10 seconds + some time on buttons creation) 

    for ( int i = 0; i < 100; i++ )
    {
        Thread.sleep ( 100 );

        final JButton button = new JButton ( "Button" + i );
        final int finalI = i;

        // Updating panel and progress in EDT
        SwingUtilities.invokeLater ( new Runnable ()
        {
            public void run ()
            {
                panel.add ( button );
                button.revalidate ();
                progressBar.setValue ( finalI );
            }
        } );
    }
}

As you can see - all the interface update operations are made in EDT, everything else runs inside the other thread.

Also notice that main thread is not EDT thread, so we can do something heavy there right away.

In some cases its not needed to display loaded parts of interface right away, so you can add them alltogether at the end of the "heavy" operation. That will save some loading time and will make the initialization code much more simple.

Brief explanation about EDT and what i said in the answer...

...it was something i found after working three years under Swing L&F and lots of Swing-based applications. I digged a lot of Swing sources and found a lot of interesting things that aren't widely known.

As you know - the whole idea of single thread for interface updates (its EDT in Swing) is about keeping each separate component visual updates (and its events) in a queue and perform them one by one inside that thread. That is needed mainly to avoid painting problems since every component inside single frame is painted to the single image that is kept in memory. The painting order is strict there so one component won't overwrite another on the final image. Painting order depends on the components tree that is created by adding some components or containers inside another container (that is a basic thing you do when creating any application interface on Swing).

To summ up - you must keep all visual updates (methods/operations that might cause them) inside the EDT. Anything else might be done outside the EDT - for example you can prepare the application interface outside the EDT (again, unless you add/remove/move component inside an already visible container).

Still there might be some internal problems with that in some very very very rare cases. There was a good discussion of that question a long ago here:
http://www.velocityreviews.com/forums/t707173-why-does-jdk-1-6-recommend-creating-swing-components-on-the-edt.html

To be short: since 6th JDK version Sun stated in docs that even Swing components creation should be done inside EDT to avoid possible problems. They might appear in some specific cases with heavy interfaces creation due to the events which occurs while the components are bing created.

Anyway, i'd say that in some cases you might create your interface outside the EDT to avoid loader/application being stuck. In other cases, when it doesn't matter if application is stuck for the interface creation time - you should use EDT. And i cannot say anything more specific since everything depends on your case...

Mikle Garin
  • 10,083
  • 37
  • 59
  • Hmm, thanks! That would work. I have one question for you though. Every official documentation + tutorials found online explicitly and repeatedly say that swing components should only be manipulated on the EDT. This is the first time I ever saw someone say that as long as they are invisible it doesn't matter. Could you please explain to me why one can break the EDT rule if components are invisible. Thanks! – whomaniac Sep 27 '12 at 08:43
  • @user1155122 i have added a description in the answer as it was too large for a single comment – Mikle Garin Sep 27 '12 at 14:29
  • I was googling online like Mad and you're the only source that gave me some actual useful information about the subject. Thanks!! – whomaniac Sep 27 '12 at 15:44
  • @user1155122 also if you are interested in advancing Swing and Graphics i recommend you to find/buy a book called "Filthy Rich Clients" (http://filthyrichclients.org/) and read it from the start till the end. You will find a complete Swing/Java2D description there - Sun/Oracle docs and other online resources will never give you such a complete and useful explanation. – Mikle Garin Sep 27 '12 at 17:28
3

Maybe you are trying to make an animation that is to be played just at the start of your application, without interfering the upcoming events or components. So you might want to give a try to splash screens. Read about it from here: http://docs.oracle.com/javase/tutorial/uiswing/misc/splashscreen.html

In the link above, it demonstrates the usage of a class named SplashScreen which is just derived from Frame class. So the mechanism is like that: you display a separate frame (splash screen, your animations go here) and after some time your main application is launched.

Juvanis
  • 25,802
  • 5
  • 69
  • 87
  • I really want to use this splash screen! I really do! However, there is a little issue. I want the splash screen to be disposed of when I want it after the main() method exits. According to the docs it exits automatically when main() exits! How can i disable this. Any ideas? – whomaniac Sep 24 '12 at 13:53
  • @user1155122: The splash screen is supposed to be disposed of when main() exists. All main() is supposed to do is start the Swing event dispatch thread, and then exit. – Gilbert Le Blanc Sep 24 '12 at 14:43
2

'ImageIcon' class allows you to load gif animations. I load the image with 'getResource()'. For doing this I normally us URL class to pass the file path. The path does not need to be necessary in a remote machine as the name URL may suggest.

URL url = this.getClass().getResource(path);
Icon myImgIcon = new ImageIcon(url);
JLabel imageLbl = new JLabel(myImgIcon);
component.add(imageLbl, BorderLayout.CENTER);

path will be the path of the gif inside of the class folder.

References: http://docs.oracle.com/javase/tutorial/uiswing/components/icon.html#getresource

Antonio
  • 851
  • 2
  • 8
  • 17
1

Inside your JFrame or whatever, use this :

Icon imgIcon = new ImageIcon(this.getClass().getResource("ajax-loader.gif"));
JLabel label = new JLabel(imgIcon);
label.setBounds(669, 42, 45, 15); // You can use your own values
frame.getContentPane().add(label);

Source : How to display an animated gif in java swing

Mehdi
  • 1,340
  • 15
  • 23