1

I'm working with Swing's ProgressMonitor.

This produces this nice dialog box for me:

enter image description here

Honestly, I think there's a flaw in the design of ProgressMonitor: What do you think happens if the user closes the dialog box by pressing the red/white cross ? Yep, you guessed it: It simply closes/hides the dialog and whatever job you have going in the background of course continues. There's no way to get the dialog box back, afaik.

Closing the window should either mean "cancel" or simply not be allowed, because I need the user to either wait for the task to complete or cancel it. How do I achieve either of this?

If I could just get a reference to that JDialog then I could do:

dialog.setDefaultCloseOperation(JDialog.DO_NOTHING_ON_CLOSE);

but I can't get that reference.

UPDATE AND FINAL ANSWER

So, I wasn't telling you the full story. In fact I use ProgressMonitorInputStream which of course under the covers use ProgressMonitor, so I thought it didn't matter for the case at hand. But it does!

Andrew is right when he says that ProgressMonitor sets the isCancelled flag when the dialog is closed. This can be verified by looking at the JDK sources. But in order to catch that event you'll need a PropertyChangeListener. When working with a ProgressMonitorInputStream you are likely not to have one (like me) because updating the ProgressMonitor is all taken care of by ProgressMonitorInputStream so you won't have to do that part yourself. I was under the assumption that ProgressMonitorInputStream's raison d'etre was in not having to do boilerplate coding.

I have to say that I find the benefits of using a ProgressMonitorInputStream to be extremely small or even negative compared to a DIY approach.

peterh
  • 18,404
  • 12
  • 87
  • 115
  • I just saw [this](http://stackoverflow.com/questions/7691629/can-i-make-progressmonitor-dialog-modal) after I posted the question. I guess it is somewhat related. As far as I read ProgressMonitors are simply not suitable for tasks where you want full control. – peterh Mar 15 '14 at 22:05
  • You might look at [`ProgressMonitorDemo`](http://docs.oracle.com/javase/tutorial/uiswing/components/progress.html#eg), which cancels the task on close. – trashgod Mar 16 '14 at 00:31
  • @trashgod. Sorry, I can't see that to be the case in `ProgressMonitorDemo`. I'm looking at file `ProgressMonitorDemo.java` and I really can't see it. – peterh Mar 16 '14 at 01:58
  • Look again. @trashgod is correct in stating it cancels the task. – Andrew Thompson Mar 16 '14 at 02:47

1 Answers1

2

Check ProgressMonitor.isCanceled(), which indeed, the ProgressMonitorDemo does.

Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433