3

We use FileDialog for open/save menus. When we save the file, first we populate the modal window using FileDialog. After user inputs the file name, the saving operation takes a long time ( up to 45 seconds) as there is a time consuming export process involved. So the problem is, during the export process, the FileDialog window is closed but there is a gray area in the FileDialog's location. Until the saving process is completed, the gray area will be cleared. Code is:

 File file = null;
 File fd = new FileDialog(mainFrame, "Save", FileDialog.SAVE);

 fd.setDirectory("./");
 fd.setLocation(50, 50);
 fd.setVisible(true);

 if (fd.getFile() != null) {
   file = new File(fd.getDirectory() + fd.getFile());
 }

 // This is a time consuming process
 ExportFromDB edb = new ExportFromDB(); 
 // Program continues

Is there any way to clear the FileDialog window completely? thanks

Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
Raistlin
  • 407
  • 1
  • 8
  • 19
  • 1
    your "export" process shall be in another thread. – RamonBoza Oct 18 '13 at 14:19
  • 1
    1) Don't block the EDT (Event Dispatch Thread) - the GUI will 'freeze' when that happens. Instead of calling `Thread.sleep(n)` implement a Swing `Timer` for repeating tasks or a `SwingWorker` for long running tasks. See [Concurrency in Swing](http://docs.oracle.com/javase/tutorial/uiswing/concurrency/) for more details. 2) The `FileDialog` is AWT, not Swing. The Swing equivalent is [`JFileChooser`](http://docs.oracle.com/javase/7/docs/api/javax/swing/JFileChooser.html). – Andrew Thompson Oct 18 '13 at 14:32

2 Answers2

2

The gray box you are seeing is because the EDT is blocked and is unable to update the GUI.

You should look into using SwingWorker to perform the long running task on a background thread.

Another option available under Java 7 is SecondaryLoop.

Take a look at Hidden Java 7 Features – SecondaryLoop for a detailed explanation & example.

Jason Braucht
  • 2,358
  • 19
  • 31
1

Your UI is going to be "Stuck" until your export finishes, the best thing to do would be to do the work in a separate thread.

Todoy
  • 1,146
  • 1
  • 9
  • 17