

The painting order is strict there so one component won't overwrite another on the final image. 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. I digged a lot of Swing sources and found a lot of interesting things that aren't widely known.Īs 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. it was something i found after working three years under Swing L&F and lots of Swing-based applications.

#Loading screen gif code#
That will save some loading time and will make the initialization code much more simple.īrief explanation about EDT and what i said in the answer. 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.
#Loading screen gif update#
SwingUtilities.invokeLater ( new Runnable ()Īs you can see - all the interface update operations are made in EDT, everything else runs inside the other thread.Īlso notice that main thread is not EDT thread, so we can do something heavy there right away. 10 seconds + some time on buttons creation)įinal JButton button = new JButton ( "Button" + i ) tBorder ( BorderFactory.createEmptyBorder ( 15, 15, 15, 15 ) ) įinal JProgressBar progressBar = new JProgressBar ( 0, 100 ) JPanel panel2 = new JPanel ( new BorderLayout () ) SwingUtilities.invokeAndWait ( new Runnable ()įinal JDialog load = new JDialog ( frame ) Here is a small example of "heavy-like" interface loading: public static void main ( String args ) throws InvocationTargetException, InterruptedExceptionįinal JPanel panel = new JPanel ( new FlowLayout ( FlowLayout.LEFT, 5, 5 ) )ĭimension ps = super.getPreferredSize () įtDefaultCloseOperation ( JFrame.EXIT_ON_CLOSE ) 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. Afterwards you have should make all changes inside the EDT, otherwise you might encounter a lot of problems. You might also create your application interface in a separate thread (yes yes, not inside the EDT) but only until you display it.

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