# Multiple Windows: How to close/exit only one or get it to only create one

**URL:** <https://discourse.processing.org/t/multiple-windows-how-to-close-exit-only-one-or-get-it-to-only-create-one/1611>\
**Category:** Coding Questions\
**Created:** [July 9, 2018, 6:12am UTC](https://discourse.processing.org/t/multiple-windows-how-to-close-exit-only-one-or-get-it-to-only-create-one/1611 "2018-07-09T06:12:37Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![jbrod](https://avatars.discourse-cdn.com/v4/letter/j/9f8e36/32.png) [@jbrod](https://discourse.processing.org/u/jbrod)\
**Post date:** [July 9, 2018, 6:12am UTC](https://discourse.processing.org/t/multiple-windows-how-to-close-exit-only-one-or-get-it-to-only-create-one/1611/1 "2018-07-09T06:12:37Z")

</div>

Hello! I am currently trying to create my own version of paint. I want my menus to be popup windows. I have two separate windows created for the two different menus. The problem I currently have is that 1) I don’t know how to get them to minimize without physically clicking the minimize button and 2) when I click ‘p’ or ‘m’ (the keys for each of the windows) it creates a whole new window so basically you end up with an infinite amount of windows. Hopefully that is not too confusing. I am open to any help, my code is below! Thanks! 😀

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

  void settings() {

    size(1000, 300);
  }

  void setup() {
    background(150);
  }

  void draw() {
    if (focused != true) {

      redraw();
    }
  }

  void mousePressed() {
     //launch("%windir%/explorer.exe shell:::{3080F90D-D7AD-11D9-BD98-0000947B0257}"); minimize
  
  }

  void keyPressed() {
    if (key == 'm' || key == 'M') {
      win = new menuWindow();
    }
  }
}

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

  void settings() {

    size(300, 800);
  }

  void setup() {
    background(150);
  }

  void draw() {
    if (focused != true) {

      redraw();
    }
  }

  void mousePressed() {
  }

  void keyPressed() {
    if (key == 'h' || key == 'H') {
      win2 = new homeWindow();
    }
  }
}

menuWindow win;
homeWindow win2;

public void settings() {
  fullScreen();
}

void setup() {
  win = new menuWindow(); //Opens main menu on start
  background(255);//Set background color
}

void draw() {
  //Left Brush
  if (mousePressed && mouseButton == LEFT) { 
    strokeWeight(8); //Sets brush thickness
    stroke(255, 0, 0); //Sets brush color
    line(mouseX, mouseY, pmouseX, pmouseY);
  }

  //Right Brush
  if (mousePressed && mouseButton == RIGHT) { // Rectangular Eraser
    strokeWeight(1); //Resets to default weight
    fill(255); //Sets fill to background color
    stroke(255); //Sets stroke to background color
    ellipse(mouseX, mouseY, 55, 55);
  }
}

void keyPressed() {

  //Opens Main Menu
  if (key == 'm' || key == 'M') {
    win = new menuWindow();
  }

  if (key == 'h' || key == 'H') {
    win2 = new homeWindow();
  }
}

```

---

<div class="post-metadata">

**Author:** ![jbrod](https://avatars.discourse-cdn.com/v4/letter/j/9f8e36/32.png) [@jbrod](https://discourse.processing.org/u/jbrod)\
**Post date:** [July 9, 2018, 6:14am UTC](https://discourse.processing.org/t/multiple-windows-how-to-close-exit-only-one-or-get-it-to-only-create-one/1611/2 "2018-07-09T06:14:40Z")

</div>

To clarify it would be helpful if there was either a way to close the specific windows so that I can call a new one or if there is a way to call the already existing window.

---

<div class="post-metadata">

**Author:** ![MxFxM](https://avatars.discourse-cdn.com/v4/letter/m/71e660/32.png) [@MxFxM](https://discourse.processing.org/u/MxFxM)\
**Post date:** [July 9, 2018, 7:17am UTC](https://discourse.processing.org/t/multiple-windows-how-to-close-exit-only-one-or-get-it-to-only-create-one/1611/3 "2018-07-09T07:17:16Z")

</div>

Hi.

Your infinite window problem is because you always create a new window when a key is pressed. You override the variable (which seems o be something like a pointer) so the old window stays and a new one is opened an will then be referenced by the variable.

To solve this you only need to create the windows once initially:

```auto
menuWindow win = new menuWindow();
homeWindow win2 = new homeWindow();

```

Then when a key is pressed you need to show / hide them (dont ask me how it works in your case, this is just an example):

```auto
void keyPressed() {

  //Opens Main Menu
  if (key == 'm' || key == 'M') {
    win.show();
  }

  if (key == 'h' || key == 'H') {
    win2.show();
  }
}

```
