> Hm, what are you doing? Display a dialog, let a thread do something?
> Without more information about such a general thing you won't get any
> useful answers here...
Well, I'll try anyway. :-)
To the original poster:
To implement cancel functionality in any long-running task that is
running in a GUI, you are going to need two threads: the UI thread
(which you have already, obviously) and the thread that will run your task.
At this stage in the game, go familiarize yourself with the Threads in
Swing article
(http://java.sun.com/products/jfc/tsc/articles/threads/threads3.html).
So: kick off a thread (in The Appropriate Non-UI-Interfering Manner as
described in the cited articles) to do your task. Have that thread
carefully update a JProgressBar or whatever GUI indication it is that
you want to take place as progress happens. This is where you have to
be careful, as the worker thread must NOT cause a GUI update to happen,
unless that GUI update happens via something like invokeLater(). That
means anytime you find yourself doing anything with a Swing class in
your worker thread you should pause and think very, very hard about what
you're doing.
Now, in the UI thread, install an ActionListener on the Cancel button.
This ActionListener will be responsible for somehow notifying the worker
thread that when it reaches a convenient stopping point it should, well,
stop. You can accomplish this via interrupt(), or, more politely, via
setting some sort of flag that your worker thread agrees to check on a
timely basis.
That should get you started, whatever it is that you're trying to do.
Hope this was useful,
Laird