wood burning stoves*
The moose likes JavaFX and the fly likes How to stop a thread when the user moves away from the JavaFX applet Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » JavaFX
Bookmark "How to stop a thread when the user moves away from the JavaFX applet" Watch "How to stop a thread when the user moves away from the JavaFX applet" New topic
Author

How to stop a thread when the user moves away from the JavaFX applet

Mathew Kuruvilla
Ranch Hand

Joined: Nov 27, 2001
Posts: 135
Scenario:
A JavaFX applet is loaded and running a thread which populates various UI elements
every 3 seconds Suddenly the user who is viewing the applet decides to open his outlook email
or some other application. At this time, the applet would still be running and updating the UI elements.

I would like to stop the thread. For this, I guess, I need to call some Javascript
function when the user moves out of the tab where the JavaFX applet is currently loaded.
How can I do this?
Thanks.
Philippe Lhoste
Greenhorn

Joined: Oct 23, 2009
Posts: 28
That's something extremely hard, or even impossible to do, at least in a cross-browser way. Unless I am proven otherwise... :-)

At one time, I just tried to detect with JavaScript when the user closes the current tab or the whole window (to logout automatically).
There are onUnload and onBeforeUnload events in JavaScript, but only some browsers support it, as the modern versions drop it: it have been too abused by JavaScript ad popups... Opera, for example, doesn't support it at all. And beside this event is triggered on a simple page refresh.

But I digress, that's not what you asked for.
Come to think of it, a possible way to explore is to use onBlur, ie. to check if the page (or the applet, perhaps) lost the focus. Not sure if that's reliable, might happen, eg., if the user just go to the browser's menu.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: How to stop a thread when the user moves away from the JavaFX applet
 
Similar Threads
applet
some problems regd threads as in velmurugan's notes
servlet and thread clarification
Insider's Guide to Mixing Swing and JavaFX
Need to handle the user opening a different application or opening a new tab