How to stop JFrame from closing after Processing sketch is closed

edited November 2017 in Questions about Code

So, I'm using Processing in a swing app and I'm wondering if there is a way to stop a JFrame from closing after the Processing sketch is stopped. This code will demonstrate my problem:

JFrameTest.pde

Frame frame;
void setup(){
  frame = new Frame();
  size(300, 300);
}

void draw(){

}

Frame.pde

import javax.swing.*;

class Frame extends JFrame {
  public Frame(){
    super("Panel");
    this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    this.setSize(250, 250);
    this.setResizable(false);

    this.setVisible( true );
  }
}

I understand that in this case, Frame is a nested class of JFrameTest in Java. In my app, this isn't the case. I use the PApplet.main method to start Processing sketches but I'm curious if there is a solution from with the PDE!

Answers

  • edited November 2017

    Thanks for pointing me to that, @GoToLoop.

    In case anyone is curious, this is what the file should look like!

    JFrameTest.pde

    Frame frame;
    void setup(){
      size(300, 300, P2D);
      stopJFrameFromClosing();
      frame = new Frame(this);
    }
    
    void draw(){
    
    }
    
    void stopJFrameFromClosing() {
      // Get OpenGL window
      com.jogamp.newt.opengl.GLWindow newtCanvas = (com.jogamp.newt.opengl.GLWindow) surface.getNative();
      // Remove listeners added by Processing
      for (com.jogamp.newt.event.WindowListener l : newtCanvas.getWindowListeners())
        if (l.getClass().getName().startsWith("processing"))
          newtCanvas.removeWindowListener(l);
      // Set on close action to dispose window
      newtCanvas.setDefaultCloseOperation(com.jogamp.nativewindow.WindowClosingProtocol.WindowClosingMode.DISPOSE_ON_CLOSE);
    }
    

    Thanks, @quark!

  • Is that proper? If I close the Frame object, it crashes. If I close the sketch, then the frame, you get a nasty messages: dead thread. Wouldn't one override their exit functions? Now, you might have re-defined your Frame class to handle the current sketch.

    Kf

Sign In or Register to comment.