ChildPApplet and closing when not disposed

Hi all,

I am trying to do a simple user interface on processing and I am running into a problem when closing a PApplet.
The main window is able to create/reopen and close the second window without problems and disposes it according to the “setDefaultClosePolicy”.
The problem is that on the second window. I have a CLOSE button to “close” the second window but if the user closes it on the top-right cross of the window instead of the button, I can’t detect that it was closed properly.
Is there a workaround on this?

Also, when trying to reopen I get the following error “OpenGL error 1281 at bot endDraw(): invalid value”.

Below is the code that I am using to test everything so far

import java.util.*;
import processing.serial.*;

ChildApplet win;
Button b;

boolean createdWindow = false;

public void settings() {
  size(320, 240);
}

void setup() { 
  surface.setLocation(20, 20);
  frameRate(40);

}

void draw() {
  background(255, 0, 0);
  fill(0);
  rect(0, 0, width/2, height);

  fill(240);
  rect(width/2, 0, width, height);

  textAlign(CENTER, CENTER);
  text("CLICK to reopen\nsecond window", width/4, height/2);

  fill(0);
  textAlign(CENTER, CENTER);
  text("CLICK to close\nsecond window", 3*width/4, height/2);
}

void mousePressed() {
  println("mousePressed in primary window");
  if (mouseX >= 0 && mouseX < width/2) {
    if (!createdWindow) { 
      win = new ChildApplet();

      createdWindow = true;
    } else {
      win.makeVisible(true);
    }
  } else win.makeVisible(false);
} 

@Override 
  void exit() {
  if (createdWindow) win.dispose();
  super.exit();
}


class ChildApplet extends PApplet {
  static final String RENDERER = P2D;
  PFont TXTfont;
  Button b;

  ChildApplet() {
    super();
    PApplet.runSketch(new String[] {this.getClass().getSimpleName()}, this);
  }

  void settings() {
    size(640, 400, RENDERER);
  }

  void setup() {
    background(0);
    setDefaultClosePolicy(this, false);
    TXTfont = createFont("Arial", 18);

    b = new Button("CLOSE", 5*width/6, 5*height/6, 50, 30, this);
    surface.setAlwaysOnTop(true);
  }

  void draw() {

    background(240);
    fill(240);
    textFont(TXTfont);
    text("Select COM Port:", 4.5*width/10, 3.5*height/10);

    b.display();
    println("running ChildApplet!");
  }

  void mousePressed() {
    println("mousePressed in secondary window");
    if (b.over()) {
      makeVisible(false);
   }
  }

  final void setDefaultClosePolicy(PApplet pa, boolean keepOpen) {
    final Object surf = pa.getSurface().getNative();
    final PGraphics canvas = pa.getGraphics();

    if (canvas.isGL()) {
      final com.jogamp.newt.Window w = (com.jogamp.newt.Window) surf;

      for (com.jogamp.newt.event.WindowListener wl : w.getWindowListeners())
        if (wl.toString().startsWith("processing.opengl.PSurfaceJOGL"))
          w.removeWindowListener(wl); 

      w.setDefaultCloseOperation(keepOpen?
        com.jogamp.nativewindow.WindowClosingProtocol.WindowClosingMode
        .DO_NOTHING_ON_CLOSE :
        com.jogamp.nativewindow.WindowClosingProtocol.WindowClosingMode
        .DISPOSE_ON_CLOSE);
    } else if (canvas instanceof processing.awt.PGraphicsJava2D) {
      final javax.swing.JFrame f = (javax.swing.JFrame)
        ((processing.awt.PSurfaceAWT.SmoothCanvas) surf).getFrame(); 

      for (java.awt.event.WindowListener wl : f.getWindowListeners())
        if (wl.toString().startsWith("processing.awt.PSurfaceAWT"))
          f.removeWindowListener(wl);

      f.setDefaultCloseOperation(keepOpen?
        f.DO_NOTHING_ON_CLOSE : f.DISPOSE_ON_CLOSE);
    }
  }


  public void makeVisible(boolean state)
  {
    surface.setVisible(state);
    println("CLOSED");
  }
}

Button class:

class Button {

  String name;
  color defaultColor, rectColor, overColor;
  float xpos, ypos, rectWidth, rectHeight, roundedCornerSize;
  PFont buttonFont;
  color c2;
  PApplet parent;

  Button(String _name, float _xpos, float _ypos, float _rectWidth, float _rectHeight, PApplet parent) {
    this.parent = parent;
    this.name = _name;
    this.xpos = _xpos;
    this.ypos = _ypos;
    this.rectWidth = _rectWidth;
    this.rectHeight = _rectHeight;
    this.roundedCornerSize = 5;
    this.buttonFont = createFont("Calibri", 15);
  }

  // Display the button
  void display() {   
    parent.rectMode(CENTER); //Draw the rectangle representing the button
    if (over()) {
      c2=color(222, 242, 252);
    } else {
      c2=color(240, 240, 240);
    }

    parent.noStroke();                //drop box inner colors
    parent.fill(c2); 
    parent.rect(xpos, ypos, rectWidth, rectHeight, roundedCornerSize);

    // Place the name of the button 
    parent.fill(0);
    parent.textFont(buttonFont);
    parent.textAlign(CENTER, CENTER);
    parent.text(this.name, this.xpos, this.ypos-2);
  }  

  // Function to determine if mouser is over the button
  boolean over() {
    return (parent.mouseX >= xpos-rectWidth/2 && parent.mouseX <= xpos+rectWidth/2 && 
      parent.mouseY >= ypos-rectHeight/2 && parent.mouseY <= ypos+rectHeight/2) ;
  }
}

Thanks in advance! :slight_smile: