0

I would like to implement some JOptionPane which displays when some process is going on, and auto-close when that process is completed. Basically, I want something like:

JOptionPane enumeratingFiles = new JOptionPane("Enumerating files...");
JDialog enumerationDialog = enumeratingFiles.createDialog(this, "Enumerating files");
enumerationDialog.setVisible(true);
// code for file enumeration
enumerationDialog.dispose();

I do not know threading, and because of that, would like to try to avoid it. However, I try the above in a JPanel in JFrame, and enumerationDialog fails to close. Is there any way to do this, without threading, or should I use Thread? If I must use Thread, how?

Mike Warren
  • 3,796
  • 5
  • 47
  • 99
  • 1
    For [example](http://stackoverflow.com/questions/25418694/opening-jdialog-with-swingworker/25419069#25419069), [example](http://stackoverflow.com/questions/13131061/safely-open-and-close-modal-jdialog-using-swingworker/13131561#13131561), [example](http://stackoverflow.com/questions/14113644/java-global-reusable-loading-dialog/14114663#14114663), [example](http://stackoverflow.com/questions/12644778/multithreading-issues-with-swing-around-dialog-create-destroy/12647582#12647582), [example](http://stackoverflow.com/questions/16632987/how-to-close-joptionpane-automatically/16633344#16633344) – MadProgrammer Oct 28 '15 at 03:56
  • 1
    The problem you have is Swing is a single threaded framework, meaning that any operation which takes time to execute will block the Event Dispatching Thread, preventing it from processing new events, including repaint events. In order to over come this, you either need to use a `Thread`, `SwingWorker` or Swing `Timer` depending on what you are trying to achieve. Swing is also not thread safe, meaning that if you update the UI, you should only do so from within the context of the EDT, which makes using `Thread`s troublesome – MadProgrammer Oct 28 '15 at 04:00
  • See See [Concurrency in Swing](http://docs.oracle.com/javase/tutorial/uiswing/concurrency/) and [Worker Threads and SwingWorker](http://docs.oracle.com/javase/tutorial/uiswing/concurrency/worker.html) for more details – MadProgrammer Oct 28 '15 at 04:00

0 Answers0