File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Threads and Synchronization and the fly likes Interrupting a Swing Thread Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Threads and Synchronization
Bookmark "Interrupting a Swing Thread" Watch "Interrupting a Swing Thread" New topic

Interrupting a Swing Thread

Fred Leaf

Joined: Oct 09, 2011
Posts: 9
I'm writing code to handle saving of the current project when the user attempts to load or create a new project or exit the program. When such an attempt is made, the user is prompted if he want to save the current project before completing the action. Currently this is handled in an inner class implementing javax.swing.Action and in an inner class implementing java.awt.WindowListener in the controller. Handling is then passed on to a method in the controller who tells the view to prompt the user and then returns a boolean whether or not a save is wanted and lets the inner class save and then take the wanted action. The problem is in the cancel-option. Part of the point of the saveFirst() method is that I don't want to have to rewrite the switch/case-block for each of new/load/exit. Therefore I would like to handle the cancel-option in that method instead. My thought here was to use Thread.currentThread().interrupt(). My knowledge of threads is however limited so I do not know how good-looking this is code-wise and if I might run into problems later so that is what I'm seeking help for.

The inner class:

The saveFirst() method in the controller

Martin Vajsar

Joined: Aug 22, 2010
Posts: 3733

This doesn't seem a good way to implement that functionality. The interrupt() method of a thread should generally be called by another thread to stop the target thread from whatever it is doing at the moment. This is not your case.

I'll describe how I usually go about this in my programs, but this is by no means the only possibility, and in all probability not the best possibility at all. You also need to figure on your own how to fit this scenario onto your object model, if you choose to do it this way.

I'd create a method called, say, canClose, which would return a boolean. This method returns true if the document can be closed, false otherwise. Whichever operation that results into closing a document (closing the window, opening a new document and so on) calls this method and if it returns false, it simply aborts the operation.

Now if the document in question is modified, the canClose method displays the standard message box ("Document was modified. Save? Yes/No/Cancel"). If the user presses No or Cancel, true or false (respectively) is immediately returned. If the user presses Yes, the document's save() method is called. If it succeeds, true is returned, if it fails (assuming a save operation can fail, which it usually can), it returns false, therefore preventing closing of a document that was not successfully saved.

You may add a parameter to the method indicating the action that is behind the call, if you want to customize the message displayed to the accordingly.
Fred Leaf

Joined: Oct 09, 2011
Posts: 9
Thank you for your reply. Yes, your implementation seem to be a much better way of solving my problem. My solution is compared to yours kinda backwards which results in the problem with the need to dismiss the thread.
I agree. Here's the link:
subject: Interrupting a Swing Thread
It's not a secret anymore!