I'm working with Swing's ProgressMonitor.
This produces this nice dialog box for me:
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.