Loading...
Logo
Processing Forum
Hi,

I'm writing a little application and for user interface purposes, I'm currently writing my code inside NetBeans, so I can directly design my JFrames and call them from the PApplet and it's cool.
My goal is to make one PApplet with everything I need inside it and call it with the static main like that :
Copy code
  1. PApplet.main(new String[]{"myPAppletClass"});
In fact it worked pretty good until I tried to add a "new" command which should launch another instance of the PApplet (like in a MDI application). But when the user closes any PApplet launched, the application completely exits !

How can I launch independant instances of the PApplet so that if one is closed the Java app goes on running with other ones ?

Thanks in advance !

Replies(3)

This should do it:

Copy code
  1. JFrame f1 = new JFrame();
  2. f1.setBounds(100, 100, 600, 200);
  3. myPAppletClass p1 = new  myPAppletClass ();
  4. p1.init();
  5. f1.add(p1);
  6. f1.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
  7. f1.setVisible(true);

  8. JFrame f2 = new JFrame();
  9. f2.setBounds(10, 10, 600, 200);
  10. myPAppletClass p2 = new  myPAppletClass ();
  11. p2.init();
  12. f2.add(p2);
  13. f2.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
  14. f2.setVisible(true);

This would create two instances of myAppletClass, adds them to two separate JFrames and closing any of the frames has no effect on the other. setDefaultCloseOperation() ensures that only the frame gets disposed, and the main program keeps on running.
Thanks for the quick answer !

Your solution actually works but the JVM doesn't exit when all windows are closed/disposed, some thread still runs even if I call myPApplet.stop(). Calling myPApplet.destroy() or .exit() exits the application and closes all the windows and - of course - that isn't what I want.

I will try to mix your solution with something like this :
Copy code
  1. myframe.addWindowListener(new java.awt.event.WindowAdapter() {
  2.     public void windowClosing(java.awt.event.WindowEvent e) {
  3.         if (openWindows == 1) {
  4.                    // Terminate when the last window is closed.
  5.             System.exit(0);  
  6.         }
  7.         g_openWindows--;
  8.     }
  9. });
to be sure that nothing goes on running in background after having closed every window. It would be nice if someone proposed a more elegant solution !
Yeah, I guess a manual check has to be done since the JFrames are set to dispose on close. Your solution is fine.
But if you could have a parent frame from which multiple child frames are launched, the parent frame could have  setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

This would also terminate the program.