# Prevent one Processing window from closing others

**URL:** <https://discourse.processing.org/t/prevent-one-processing-window-from-closing-others/13719>\
**Category:** Coding Questions\
**Created:** [August 31, 2019, 10:54am UTC](https://discourse.processing.org/t/prevent-one-processing-window-from-closing-others/13719 "2019-08-31T10:54:29Z")\
**Posts on this page:** 9\
**Page:** 1

<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:** [August 31, 2019, 10:54am UTC](https://discourse.processing.org/t/prevent-one-processing-window-from-closing-others/13719/1 "2019-08-31T10:54:29Z")

</div>

I’m experimenting with launching multiple processing windows.

I find that when I set the renderer to be the same for all sketch instances, closing one immediately closes the other two instances.

When all renderers are different (as per the code example) the behavior changes: when the `FX2D` sketch is closed, the others remain open; when the `JAVA2D` sketch is closed, the other two freeze for a moment and then both close; when the `P2D` sketch is closed, the `JAVA2D` sketch closes immediately followed by the `FX2D` sketch very shortly afterwards.

_This behavior seems to be independent of the order in which the sketches (the renderers) are launched using `runSketch()`._

**I want the program to behave such that closing any sketch (window) does not affect the others (they will not also close). Can this be done?**

* * *

This is the code I’m playing with:

```auto
public static void main(String[] args) {

	Sketch sketch1 = new Sketch(PApplet.FX2D);
	Sketch sketch2 = new Sketch(PApplet.P2D);
	Sketch sketch3 = new Sketch(PApplet.JAVA2D);
	PApplet.runSketch(new String[] { "--location=760,0", "" }, sketch1);
	PApplet.runSketch(new String[] { "--location=760,80", "" }, sketch2);
	PApplet.runSketch(new String[] { "--location=760,160", "" }, sketch3);
}

```

The `Sketch` class has the following code (the renderer is set during initialization):

```auto
public final class Sketch extends PApplet {

	private final String renderer;

	public Sketch(String renderer) {
		this.renderer = renderer;
	}

	@Override
	public void settings() {
		size(800, 800, renderer);
	}
    .
    .
    .

```

---

<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:** [August 31, 2019, 11:14am UTC](https://discourse.processing.org/t/prevent-one-processing-window-from-closing-others/13719/2 "2019-08-31T11:14:31Z")

</div>

- [Forum.Processing.org/two/discussion/17310/intercept-window-closure-in-p2d-renderer-processing-3#Item\_5](http://Forum.Processing.org/two/discussion/17310/intercept-window-closure-in-p2d-renderer-processing-3#Item_5)

---

<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:** [August 31, 2019, 11:27am UTC](https://discourse.processing.org/t/prevent-one-processing-window-from-closing-others/13719/3 "2019-08-31T11:27:55Z")

</div>

This thread looks at how to prevent one from closing a window. I want to maintain that functionality – the problem I (/ the Processing library) have is closing one window is closing other windows (there is a coupling somewhere).

---

<div class="post-metadata">

**Author:** ![quark](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/quark/32/26_2.png) [@quark](https://discourse.processing.org/u/quark)\
**Post date:** [August 31, 2019, 12:03pm UTC](https://discourse.processing.org/t/prevent-one-processing-window-from-closing-others/13719/4 "2019-08-31T12:03:48Z")

</div>

All three of your sketches are being launched by another Processing sketch (the enclosing class) so they are all coupled through that. Processing’s default action on window close is to exit the application which will close the other windows. In the link provided by @GoToLoop you see that you have remove the existing windows listeners and add your own.

---

<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:** [August 31, 2019, 7:11pm UTC](https://discourse.processing.org/t/prevent-one-processing-window-from-closing-others/13719/5 "2019-08-31T19:11:36Z")

</div>

_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` sketches: the window is not disposed properly when the cross is clicked.

**Solution:**

_Override `exitActual()` to prevent the JVM (and all sketches) closing when one sketch is closed._

```auto
@Override
public void exitActual() {
}

```

_Introduce the following code (suitable in `setup()`) such that `P2D` sketches are disposed properly:_

```auto
if (getGraphics().isGL()) {
		final com.jogamp.newt.Window w = (com.jogamp.newt.Window) getSurface().getNative();
		w.setDefaultCloseOperation(WindowClosingMode.DISPOSE_ON_CLOSE);
}

```

* * *

**However** , in overriding (preventing) the call to `System.exit(0)`, another issue is introduced: even after all sketches (windows) are closed, the JVM will continue to run in memory. Ideally `System.exit(0)` should be called but only when the last remaining window is disposed.

I propose a rudimentary fix below – a simple counter – but I wonder whether it is possible to inspect the JVM to see the number of live instances of JavaFX/NEWT/AWT windows, using those values instead?

```auto
@Override
public void exitActual() {
	if (Benchmark.n == 1) {
			System.exit(0);
	}
	Benchmark.n--;
}

```

```auto
public class Benchmark {

	public static int n = 3;

	public static void main(String[] args) {

		Sketch sketch1 = new Sketch(PApplet.FX2D);
		Sketch sketch2 = new Sketch(PApplet.P2D);
		Sketch sketch3 = new Sketch(PApplet.JAVA2D);
		PApplet.runSketch(new String[] { "--location=760,0", "" }, sketch1);
		PApplet.runSketch(new String[] { "--location=760,80", "" }, sketch2);
		PApplet.runSketch(new String[] { "--location=760,160", "" }, sketch3);
	}
}

```

---

<div class="post-metadata">

**Author:** ![chuchu](https://avatars.discourse-cdn.com/v4/letter/c/13edae/32.png) [@chuchu](https://discourse.processing.org/u/chuchu)\
**Post date:** [May 14, 2020, 2:02pm UTC](https://discourse.processing.org/t/prevent-one-processing-window-from-closing-others/13719/6 "2020-05-14T14:02:46Z")

</div>

Hi!

I am trying to avoid ThreadDeath when closing 2 P3D windows, I have inserted micycle’s code into the multiwindow example but I am getting “The variable “WindowClosingMode” does not exist” error. Can anyone tell me what I am doing wrong?

First Window

```auto
PWindow win;

@Override
  public void exitActual() {
}

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

void setup() { 
  win = new PWindow();
  
  if (getGraphics().isGL()) {
    final com.jogamp.newt.Window w = (com.jogamp.newt.Window) getSurface().getNative();
    w.setDefaultCloseOperation(WindowClosingMode.DISPOSE_ON_CLOSE);
  }
}

void draw() {
  background(255, 0, 0);
  fill(255);
  rect(10, 10, frameCount, 10);
} 

```

Second Window

```auto
class PWindow extends PApplet {
  PWindow() {
    super();
    PApplet.runSketch(new String[] {this.getClass().getSimpleName()}, this);
  }

  void settings() {
    size(500, 200, P3D);
  }

  void setup() {
    background(150);

    if (getGraphics().isGL()) {
      final com.jogamp.newt.Window w = (com.jogamp.newt.Window) getSurface().getNative();
      w.setDefaultCloseOperation(WindowClosingMode.DISPOSE_ON_CLOSE);
    }
  }

  void draw() {
    ellipse(random(width), random(height), random(50), random(50));
  }
}

```

Ta

---

<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:** [May 14, 2020, 7:12pm UTC](https://discourse.processing.org/t/prevent-one-processing-window-from-closing-others/13719/7 "2020-05-14T19:12:05Z")

</div>

> [@chuchu](#):
>
> WindowClosingMode

You need to include it as an import statement, though I’m not sure of the exact class path.

---

<div class="post-metadata">

**Author:** ![Flamingo](https://avatars.discourse-cdn.com/v4/letter/f/919ad9/32.png) [@Flamingo](https://discourse.processing.org/u/Flamingo)\
**Post date:** [March 21, 2021, 1:35pm UTC](https://discourse.processing.org/t/prevent-one-processing-window-from-closing-others/13719/8 "2021-03-21T13:35:29Z")

</div>

Follow up: has anyone found the WindowClosingMode import statement and class path. I’m having a hard time with it.

---

<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 21, 2021, 3:01pm UTC](https://discourse.processing.org/t/prevent-one-processing-window-from-closing-others/13719/9 "2021-03-21T15:01:52Z")

</div>

`import com.jogamp.nativewindow.WindowClosingProtocol.WindowClosingMode;`
