Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / GUI / April 2004

Tip: Looking for answers? Try searching our database.

How to implement Cancel functionality

Thread view: 
richard green - 21 Apr 2004 22:13 GMT
Is there any pattern which allows to implement the Cancel
functionality from the GUI. Basically a user can press the Cancel
button anytime and a long processing job should immediately do the
required cleanup and finish. How to do it?

Thanx in Advance,
Richard
Carsten Ringe - 22 Apr 2004 11:35 GMT
>Is there any pattern which allows to implement the Cancel
>functionality from the GUI. Basically a user can press the Cancel
>button anytime and a long processing job should immediately do the
>required cleanup and finish. How to do it?
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...

Carsten Ringe
Leben auf dem Land? Warum auch nicht.

http://www.landleben-in-thal.de.vu/
Laird Nelson - 22 Apr 2004 18:43 GMT
> 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


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.