Preventing a PApplet closing the calling Java program

edited April 2016 in How To...

Ok, so I have this Java Program developed primarily in Swing (Enthusiasm yay!). A small part of it uses Unfolding, a processing library for create maps (like geographical maps). Anyway, all of this is built in a class called MapProcessingApplet an extension of PApplet.

Now to open this window from the calling window I implement and actionlistener on a button, await the click and then do the following:

MapProcessingApplet mapApplet = new MapProcessingApplet();
PApplet.runSketch(new String[]{"<Package>.MapProcessingApplet"}, mapApplet);

(Where the is actually configured correctly).

Now, this runs fine, the window opens, it shows a rather lovely map (thank you, unfolding), but when I click the close button (the cross in the top right) it doesn't just close the PApplet window, it also closes the window from which I call it and terminates the entire program.

Is there a way to prevent it from doing this?

(You know, because kamikaze maps are cool and all, but not really what the end user is looking for in a program)

Answers

  • Thank you, except I think I'm using processing 2 not processing 3, I am assuming I remove window listeners from the frame rather than the surface right?

  • Yes remove them from the frame.

  • edited March 2016

    Variable frame in both P2 & P1 is already a JFrame.
    I guess it's very easy to adapt my solution to old Processing versions. :D

    http://docs.Oracle.com/javase/8/docs/api/javax/swing/JFrame.html

  • the frame variable is actual just a Frame although it can be cast to a JFrame. I cast it to JFrame and did:

    ((JFrame) frame).setDefaultCloseOperation(HIDE_ON_CLOSE);

    with no success, also removed the WindowListeners

  • edited March 2016

    The frame variable is actual just a Frame although it can be cast to a JFrame.

    Indeed field frame is declared as datatype Frame. However it points to a JFrame object instead. :P

    ... with no success, ...

    My solution relies on class Window's removeWindowListener() method: :-B

    http://docs.Oracle.com/javase/8/docs/api/java/awt/Window.html#removeWindowListener-java.awt.event.WindowListener-

    static final void removeExitEvent(final PSurface surf) {
      final java.awt.Window win
        = ((processing.awt.PSurfaceAWT.SmoothCanvas) surf.getNative()).getFrame();
    
      for (final java.awt.event.WindowListener evt : win.getWindowListeners())
        win.removeWindowListener(evt);
    }
    
  • Ok, but PSurface doesn't seem to be part of the Processing 2 Core library

  • Did you remove all WindowListeners?

    If you have removed them all you can try the DO_NOTHING_ON_CLOSE option and add your own WindowListener/ WindowAdapter to handle the window closing event.

  • Actually that's probably a more elegant solution, I'll try attaching my own WindowListener event. The DO_NOTHING_ON_CLOSE seems to be having no effect.

  • Ok, so removing all the WindowListeners and attaching my own to open a new window hasn't worked either. It kills even the newly open window

  • It is the approach I took in the G4P library and it worked for me. :)

  • edited March 2016

    Ok, but PSurface doesn't seem to be part of the Processing 2 Core library...
    Thank you, except I think I'm using Processing 2 not Processing 3, ...

    • You've already confirmed you're using P2.
    • I have already told you that you need to port my P3 solution to P2.
    • And I have already told you that frame already points to a JFrame object.
  • Ok, well I've attempted to port you solution. I created a loop for removing all the WindowListeners from the JFrame. I ran this program in a Debugging session and did a variable watch on the JFrame in PApplet.

    Upon inspection of the WindowListeners within this frame, there were no windowListeners set to the PApplet.

    As far as I can tell all windowListeners have been removed from the frame. The default close operation was also set on both "HIDE ON CLOSE" and "DO NOTHING ON CLOSE"

Sign In or Register to comment.