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.

有帮助吗?

解决方案

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.

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top