We are about to switch to a new forum software. Until then we have removed the registration on this forum.
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
https://forum.Processing.org/two/discussion/12319/using-papplet-runsketch-to-create-multiple-windows-in-a-ps3-sketch#Item_25
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.
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
Indeed field frame is declared as datatype Frame. However it points to a JFrame object instead. :P
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-
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. :)
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"