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 › Graceful exit from an exported application
Page Index Toggle Pages: 1
Graceful exit from an exported application? (Read 1992 times)
Graceful exit from an exported application?
May 27th, 2010, 9:22am
 
Is there a way to circumvent the automatic closing of an exported "application.macosx"  or "application.windows".   I would like to force the user to have the option of saving to file (using saveStrings() ) before exiting the application.  In my code I provide for this, but if the user simply clicks the "close button" in the top left-hand corner of the window, all is lost.  

Is there a way to intecept that closing, so that I can prompt the user?
Re: Graceful exit from an exported application?
Reply #1 - May 28th, 2010, 6:31am
 
I think you can achieve this with a custom stop() function.  You just need to include super.stop(); at some point to actually end the program.
Re: Graceful exit from an exported application?
Reply #2 - May 28th, 2010, 6:55am
 
I was curious about this, so I tried it myself:

Quote:
void setup() {
  size(200, 200);
}

void draw() {
  background(0);
}

void stop() {
  String savePath = selectOutput();
  super.stop();
}


When I run this from the PDE, clicking on the window's close widget generates a save prompt, as it should.  But when I export it to an application, it closes without a save prompt.

Re: Graceful exit from an exported application?
Reply #3 - May 28th, 2010, 7:59am
 
I believe this is something of a FAQ.  IIRC the issue is that to interrupt the 'close' button on an exported app you need to do things at the OS level; and that's where things become less straightforward.

Probably worth searching the forums for a more comprehensive answer.
Re: Graceful exit from an exported application?
Reply #4 - May 28th, 2010, 11:38am
 
what blindfish said.  You need to use noLoop() and loop() around your output selection dialog, e.g. :

Code:
  noLoop();
 String savePath=selectOutput() + ".png";
 if(savePath!=null) save(savePath);
 loop();
Re: Graceful exit from an exported application?
Reply #5 - May 28th, 2010, 11:51am
 
@ben_hem: I don't think that's necessary, Processing takes care of such problems for the functions it provides. The trick is necessary to use JFileChooser, thought (or other Swing dialogs, like the color chooser).
Page Index Toggle Pages: 1