# Closing only PApplet with exit() code

**URL:** https://discourse.processing.org/t/closing-only-papplet-with-exit-code/18591
**Category:** Coding Questions
**Created:** [March 11, 2020, 10:08am UTC](https://discourse.processing.org/t/closing-only-papplet-with-exit-code/18591 "2020-03-11T10:08:36Z")
**Posts on this page:** 6
**Page:** 1

<div class="post-metadata">

### Author: ![GeorgeJava](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/georgejava/32/7646_2.png) [@GeorgeJava](https://discourse.processing.org/u/GeorgeJava)
#### Post date: [March 11, 2020, 10:08am UTC](https://discourse.processing.org/t/closing-only-papplet-with-exit-code/18591/1 "2020-03-11T10:08:36Z")

</div>

Hello, i want to close only PApplet, with close button and with self exit() event when i do somethink, for exemple in this, i just click, and want to end only “AnotherWindow” applet, how to do it ?

```auto
PApplet cf;

void settings() {
  size(800, 450, P3D);
}

void setup() {
  cf = new AnotherWindow();
}

void draw() {
  background((sin(millis()/750.0)+1)/2*255);
}

class AnotherWindow extends PApplet {
  int w, h;
  public AnotherWindow() {
    super();   
    this.w = 350;
    this.h = 350;
    PApplet.runSketch(new String[]{this.getClass().getSimpleName()}, this);
  }
  public void settings() {
    size(w, h, P3D);
  }
  void setup() {
  }
  void draw() {
    background(0, 255, 0);
  }
  void mousePressed() {
    this.exit();
  }
}

```

---

<div class="post-metadata">

### Author: ![GoToLoop](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/gotoloop/32/86_2.png) [@GoToLoop](https://discourse.processing.org/u/GoToLoop)
#### Post date: [March 11, 2020, 9:57pm UTC](https://discourse.processing.org/t/closing-only-papplet-with-exit-code/18591/2 "2020-03-11T21:57:24Z")

</div>

> [@Closing a second window](https://discourse.processing.org/t/closing-a-second-window/381/3):
>
> [https://Forum.Processing.org/two/discussion/17310/intercept-window-closure-in-p2d-renderer-processing-3#Item\_5](https://Forum.Processing.org/two/discussion/17310/intercept-window-closure-in-p2d-renderer-processing-3#Item_5)

---

<div class="post-metadata">

### Author: ![GeorgeJava](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/georgejava/32/7646_2.png) [@GeorgeJava](https://discourse.processing.org/u/GeorgeJava)
#### Post date: [March 12, 2020, 11:20am UTC](https://discourse.processing.org/t/closing-only-papplet-with-exit-code/18591/3 "2020-03-12T11:20:45Z")

</div>

When i want to exit in mousePressed, it stop and get error and freez

**CODE:**

```auto
AnotherWindow cf;

void settings() {
  size(800, 450, P3D);
}

void setup() {
  cf = new AnotherWindow();
  setDefaultClosePolicy(this, true);
}

void draw() {
  background((sin(millis()/750.0)+1)/2*255);
}

class AnotherWindow extends PApplet {
  int w, h;
  public AnotherWindow() {
    super();   
    this.w = 350;
    this.h = 350;
    PApplet.runSketch(new String[]{this.getClass().getSimpleName()}, this);
  }
  public void settings() {
    size(w, h, P3D);
  }
  void setup() {
    setDefaultClosePolicy(this, true);
  }
  void draw() {
    background(0, 255, 0);
  }
  void mousePressed() {
    cf.exit();
  }
}

void keyPressed() {
  final int k = keyCode;
  if (k == ESC) key = 0;
}

static 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);
  }
}

```

 ![image](https://canada1.discourse-cdn.com/flex036/uploads/processingfoundation1/original/2X/e/e9f9e3af145e9e7960f9c525a4380cc15a07afb3.png)

---

<div class="post-metadata">

### Author: ![neilcsmith](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/neilcsmith/32/144_2.png) [@neilcsmith](https://discourse.processing.org/u/neilcsmith)
#### Post date: [March 12, 2020, 2:15pm UTC](https://discourse.processing.org/t/closing-only-papplet-with-exit-code/18591/4 "2020-03-12T14:15:54Z")

</div>

Running multiple windows using the OpenGL renderer has a tendency to crash, particularly when closing one of the PApplets. This is mainly due to use of shared static state. I reported and offered to help fix this ages ago but was told it’s unsupported and a won’t-fix. 🙄

---

<div class="post-metadata">

### Author: ![GeorgeJava](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/georgejava/32/7646_2.png) [@GeorgeJava](https://discourse.processing.org/u/GeorgeJava)
#### Post date: [March 16, 2020, 6:53pm UTC](https://discourse.processing.org/t/closing-only-papplet-with-exit-code/18591/5 "2020-03-16T18:53:05Z")

</div>

Soo ? How to make other window with PApplet or draw func. with other solution ?

---

<div class="post-metadata">

### Author: ![micycle](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/micycle/32/201_2.png) [@micycle](https://discourse.processing.org/u/micycle)
#### Post date: [March 17, 2020, 9:33am UTC](https://discourse.processing.org/t/closing-only-papplet-with-exit-code/18591/6 "2020-03-17T09:33:11Z")

</div>

> [@Prevent one Processing window from closing others](https://discourse.processing.org/t/prevent-one-processing-window-from-closing-others/13719/5):
>
> I’ve done some digging – inspired by @GoToLoop and @quark – and have found a solution. Closing sketches in P2D and JAVA2D renderer modes calls the PApplet instance’s exitActual() method, which contains the line System.exit(0) – a static method – which terminates the currently running Java virtual machine. Overriding exitActual() with nothing (i.e. preventing the call to System.exit(0)) prevents sketches from closing others, fixing the issue, but introduces another problem in the case of P2D s…
