Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8617581
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T05:53:42+00:00 2026-06-12T05:53:42+00:00

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

  • 0

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?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-12T05:53:43+00:00Added an answer on June 12, 2026 at 5:53 am

    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…

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Hello I'm writing an application that uses the compatibility library and i'm using a
hello I have some question about writing class in Java, why this one is
Hello Fellow stackoverflowers, I´m stuck writing a piece of code. I have application with
I’m writing a client application with an accompanying GUI (Swing). My two classes, ClientClass
Hello I am writing some data structures in C, and I've realized that their
I have lots of experience of writing php scripts that are run in the
I have a main thread that contains my WPF GUI and one or more
Hello (this is a long post sorry), I am writing a application in ASP.NET
I'm writing a method which, let's say, given 1 and hello should return http://something.com/?something=1&hello=en
Hello I'm an amateur trying to learn/improve my understanding of Java by writing a

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.