We closed this forum 18 June 2010. It has served us well since 2005 as the ALPHA forum did before it from 2002 to 2005. New discussions are ongoing at the new URL http://forum.processing.org. You'll need to sign up and get a new user account. We're sorry about that inconvenience, but we think it's better in the long run. The content on this forum will remain online.
IndexProgramming Questions & HelpSyntax Questions › Call a function before applet exit
Page Index Toggle Pages: 1
Call a function before applet exit (Read 688 times)
Call a function before applet exit
Apr 2nd, 2009, 5:16am
 
Hi there, i'd like to make a function that is called when the user navigates away from the applet window on my website. Is this possible? Is navigating away the same as closing the window of a non-web-based applet? If so, could I use java.awt.event.WindowAdapter and windowListeners?
Thanks,
Rob
Re: Call a function before applet exit
Reply #1 - Apr 2nd, 2009, 11:43am
 
In a MySQL sketch, I use this code to close the connection:
Code:
void stop()
{
println("Stopping connection");
mysql.close();
}

The stop() method is called when the sketch closes. Although I am not sure it is properly called when it is an applet embedded in a browser. And it is hard to verify, except by sending a signal to a server perhaps.
Re: Call a function before applet exit
Reply #2 - Apr 3rd, 2009, 4:00am
 
You should probably add in super.stop(); at the end of that function so Processing's stop code still gets called.
Re: Call a function before applet exit
Reply #3 - Apr 3rd, 2009, 4:42am
 
Excellent advice, JohnG!
I checked PApplet code, and indeed it does some clean-up stuff (like g.dispose()).
I also saw the comment:
  * Called by the browser or applet viewer to inform
  * this applet that it should stop its execution.
  * <p/>
  * Unfortunately, there are no guarantees from the Java spec
  * when or if stop() will be called (i.e. on browser quit,
  * or when moving between web pages), and it's not always called.
which confirms there is no guarantee of call on browser/page/tab closing/moving away.
Page Index Toggle Pages: 1