# Limit window maximum size

**URL:** https://discourse.processing.org/t/limit-window-maximum-size/27076
**Category:** Coding Questions
**Created:** [January 15, 2021, 6:33am UTC](https://discourse.processing.org/t/limit-window-maximum-size/27076 "2021-01-15T06:33:53Z")
**Posts on this page:** 6
**Page:** 1

<div class="post-metadata">

### Author: ![mgood7123](https://avatars.discourse-cdn.com/v4/letter/m/54ee81/32.png) [@mgood7123](https://discourse.processing.org/u/mgood7123)
#### Post date: [January 15, 2021, 6:33am UTC](https://discourse.processing.org/t/limit-window-maximum-size/27076/1 "2021-01-15T06:33:53Z")

</div>

is there a way to set a maximum window size?

in particular I am trying to prevent content overflow by limiting the maximum window size

```auto
void setup() {
  // initial window size
  size(640, 360, P2D);

  // allow window to be resized
  surface.setResizable(true);
  
  view.setup(P2D);
  callOnSizeChanged(view, 720, 480);
  
  verticalScrollBar.onOverflow = verticalScrollBar.new OverflowRunnable() {
    @Override
    public void run(int value) {
      size(width, value, P2D);
    }
  };

  horizontalScrollBar.onOverflow = horizontalScrollBar.new OverflowRunnable() {
    @Override
    public void run(int value) {
      size(value, height, P2D);
    }
  };

  verticalScrollBar.setup(P2D);
  horizontalScrollBar.setup(P2D);

  verticalScrollBar.document = view;
  horizontalScrollBar.document = view;
}

```

but I get

```auto
When not using the PDE, size() can only be used inside settings().
Remove the size() method from setup(), and add the following:
public void settings() {
  size(720, 472, "processing.opengl.PGraphics2D");
}
IllegalStateException: size() cannot be used here, see https://processing.org/reference/size_.html

```

at

```auto
   horizontalScrollBar.onOverflow = horizontalScrollBar.new OverflowRunnable() {
     @Override
     public void run(int value) {
> size(value, height, P2D);
     }
   };

```

---

<div class="post-metadata">

### Author: ![CodeMasterX](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/codemasterx/32/2942_2.png) [@CodeMasterX](https://discourse.processing.org/u/CodeMasterX)
#### Post date: [January 15, 2021, 7:09pm UTC](https://discourse.processing.org/t/limit-window-maximum-size/27076/2 "2021-01-15T19:09:48Z")

</div>

```auto
if(width > maxW) surface.setSize(maxW,height);
if(height > maxH) surface.setSize(width,maxH);

```

---

<div class="post-metadata">

### Author: ![mgood7123](https://avatars.discourse-cdn.com/v4/letter/m/54ee81/32.png) [@mgood7123](https://discourse.processing.org/u/mgood7123)
#### Post date: [January 15, 2021, 8:04pm UTC](https://discourse.processing.org/t/limit-window-maximum-size/27076/3 "2021-01-15T20:04:57Z")

</div>

I already tried that but it just glitches when resizing with mouse

---

<div class="post-metadata">

### Author: ![r.jaoui](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/r.jaoui/32/11777_2.png) [@r.jaoui](https://discourse.processing.org/u/r.jaoui)
#### Post date: [January 15, 2021, 9:33pm UTC](https://discourse.processing.org/t/limit-window-maximum-size/27076/4 "2021-01-15T21:33:22Z")

</div>

In a previous code, I ran this in the beginning of _draw()_ (this is for a minimum size, not a maximum, but the idea should be similar) :

```auto
GLWindow window = (GLWindow) getSurface().getNative();

final Dimension d = new Dimension(MIN_SIZE_X + 17, MIN_SIZE_Y + 40);

int xDist = max(window.getWidth(), d.width )-window.getWidth ();
int yDist = max(window.getHeight(), d.height)-window.getHeight();

window.setSize(max(window.getWidth(), d.width), max(window.getHeight(), d.height));
window.setPosition(window.getLocationOnScreen(null).getX() - xDist, window.getLocationOnScreen(null).getY() - yDist);

```

And although this works, It is quite glichy… The thing is, anyone correct me if I’m wrong, but I don’t think that P2D PGraphics handle well resizing in general… it’s like the sketch stops when resizing, and no call to _draw()_ is made…

Good luck !

---

<div class="post-metadata">

### Author: ![mgood7123](https://avatars.discourse-cdn.com/v4/letter/m/54ee81/32.png) [@mgood7123](https://discourse.processing.org/u/mgood7123)
#### Post date: [January 16, 2021, 1:57am UTC](https://discourse.processing.org/t/limit-window-maximum-size/27076/5 "2021-01-16T01:57:51Z")

</div>

wish there was a better way to set window size limits ☹

(and a way that actually works without having glitches such as the mouse overriding set size when resizable is true and the window is being resized)

---

<div class="post-metadata">

### Author: ![CodeMasterX](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/codemasterx/32/2942_2.png) [@CodeMasterX](https://discourse.processing.org/u/CodeMasterX)
#### Post date: [January 16, 2021, 11:24am UTC](https://discourse.processing.org/t/limit-window-maximum-size/27076/6 "2021-01-16T11:24:21Z")

</div>

just create an `int wdth and hght` (for width and height) and after you stop resizing the window, the variables are set to it. Because of that it won’t be buggy.
