# Closing a second window

**URL:** <https://discourse.processing.org/t/closing-a-second-window/381>\
**Category:** Coding Questions\
**Created:** [May 25, 2018, 7:55pm UTC](https://discourse.processing.org/t/closing-a-second-window/381 "2018-05-25T19:55:02Z")\
**Posts on this page:** 1\
**Showing post:** 2

<div class="post-metadata">

**Author:** ![colouredmirrorball](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/colouredmirrorball/32/234_2.png) [@colouredmirrorball](https://discourse.processing.org/u/colouredmirrorball)\
**Post date:** [May 25, 2018, 8:02pm UTC](https://discourse.processing.org/t/closing-a-second-window/381/2 "2018-05-25T20:02:12Z")

</div>

This did the trick for me:

```auto

PWindow win;

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

void setup() { 
  surface.setLocation(20,20);
  win = new PWindow();
}

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

void mousePressed() {
  println("mousePressed in primary window");
  if(win == null) win = new PWindow();
} 

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

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

  void setup() {
    background(150);
  }

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

  void mousePressed() {
    println("mousePressed in secondary window");
  }
  
  void exit()
  {
    dispose();
    win = null;
  }
}

```

Clicking in the first window when the second is closed will also open a new secondary window.

---

_[View the full topic](https://discourse.processing.org/t/closing-a-second-window/381)._
