Question

I am trying to create a JDialog which will shows an animated image till background process gets completed. I created a JDialog which shows the Animated GIF but when I put it before the actual process get started it shows only blank JDialog.

Here is my code:

public void createDialog()
{
d=new JDialog();
JLabel l=new JLabel(new ImageIcon(new URL("path_of_icon")));
d.setSize(100,100);
d.add(l);
d.setVisible(true);
d.alwaysOnTop(true);
}

//now how do I calling this function

createDialog();
doBackgroundProcess(); //like fetching data from database
d.dispose();

Can anybody tell me how do I show the loading image without interrupting the background process. Do I need to use SwingWorker or Thread.

Was it helpful?

Solution

SwingWorker is your best choice, because it will manage several threads for you.

Here is more about it http://docs.oracle.com/javase/tutorial/uiswing/concurrency/worker.html

UPDATE:

doInBackground is executed on a separate (not a UI) thread and usually used for a long non-UI process. Method done is executed on UI thread when the long process is finished. So create and show your dialog before creating SwingWorker. Than run your background process using SwingWorker, in done - hide the dialog.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top