# Change size of G4P controls

**URL:** https://discourse.processing.org/t/change-size-of-g4p-controls/30722
**Category:** Libraries
**Created:** [June 15, 2021, 1:51pm UTC](https://discourse.processing.org/t/change-size-of-g4p-controls/30722 "2021-06-15T13:51:49Z")
**Posts on this page:** 15
**Page:** 1

<div class="post-metadata">

### Author: ![gyula](https://avatars.discourse-cdn.com/v4/letter/g/ccd318/32.png) [@gyula](https://discourse.processing.org/u/gyula)
#### Post date: [June 15, 2021, 1:51pm UTC](https://discourse.processing.org/t/change-size-of-g4p-controls/30722/1 "2021-06-15T13:51:49Z")

</div>

I’d like to be able to rearrange my G4P controls with the mouse. I’ve found an easy way to move them around with `moveTo(newX, newY)`:

```auto
//G4P library
import g4p_controls.*;

GButton button;
GTextField textField;

ArrayList<GAbstractControl> controls = new ArrayList<GAbstractControl>();

public void setup() {
  size(480, 320);

  button = new GButton(this, 188, 145, 80, 30);
  button.setText("button");
  controls.add(button); // upcast to GAbstractControl

  textField = new GTextField(this, 188, 205, 80, 20);
  textField.setText("textfield");
  controls.add(textField); // upcast to GAbstractControl
}

public void draw() {
  background(230);
}

void mouseDragged() {

  for ( GAbstractControl control : controls) {
    if ( control.isDragging() ) {
      float x = control.getX() + mouseX - pmouseX;
      float y = control.getY() + mouseY - pmouseY;
      control.moveTo(x, y);
    }
  }
}

```

But how would I change the width and height of the controls?

Thanks.

---

<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: [June 15, 2021, 4:08pm UTC](https://discourse.processing.org/t/change-size-of-g4p-controls/30722/2 "2021-06-15T16:08:40Z")

</div>

Sorry but G4P does not support resizing controls after creation.

---

<div class="post-metadata">

### Author: ![gyula](https://avatars.discourse-cdn.com/v4/letter/g/ccd318/32.png) [@gyula](https://discourse.processing.org/u/gyula)
#### Post date: [June 16, 2021, 8:07am UTC](https://discourse.processing.org/t/change-size-of-g4p-controls/30722/3 "2021-06-16T08:07:20Z")

</div>

Thanks, Peter.

> G4P does not support resizing controls after creation.

Then would it be overkill or even possible to somehow delete the control and **recreate** it with its original properties but in a new size without losing its `addEventHandler()`?

---

<div class="post-metadata">

### Author: ![paulgoux](https://avatars.discourse-cdn.com/v4/letter/p/b9bd4f/32.png) [@paulgoux](https://discourse.processing.org/u/paulgoux)
#### Post date: [June 16, 2021, 8:14am UTC](https://discourse.processing.org/t/change-size-of-g4p-controls/30722/4 "2021-06-16T08:14:24Z")

</div>

Shamless plu, currently working on a lib, Still some adjustments left as its in beta, but it should be able to handle what you require.

> **[paulgoux/Windows-Gui-Library](https://github.com/paulgoux/Windows-Gui-Library)**
>
> Windows Gui Library. Contribute to paulgoux/Windows-Gui-Library development by creating an account on GitHub.

I can paste example code if you want to learn more.

---

<div class="post-metadata">

### Author: ![gyula](https://avatars.discourse-cdn.com/v4/letter/g/ccd318/32.png) [@gyula](https://discourse.processing.org/u/gyula)
#### Post date: [June 16, 2021, 12:30pm UTC](https://discourse.processing.org/t/change-size-of-g4p-controls/30722/5 "2021-06-16T12:30:55Z")

</div>

> I can paste example code if you want to learn more.

I’m committed to G4P, still I would be interested in your example code.

---

<div class="post-metadata">

### Author: ![paulgoux](https://avatars.discourse-cdn.com/v4/letter/p/b9bd4f/32.png) [@paulgoux](https://discourse.processing.org/u/paulgoux)
#### Post date: [June 16, 2021, 12:56pm UTC](https://discourse.processing.org/t/change-size-of-g4p-controls/30722/6 "2021-06-16T12:56:44Z")

</div>

```auto
BMS b;
	Button b1,b2,b3;
	boolean bb1 = false;
	
	public void settings() {
		// fullScreen();
		size(700, 500);
		
	};

	public void setup() {
		b = new BMS(this);
		b1 = new Button(20,90,90,20,"BTN1",b);
		b.add(b1);
	};
	
	public void draw() {
		background(255);
		b.runEmpty(mouseButton);
		
		
		if(b1.click)println("click",b1.toggle);
		b1.toggle(this,"bb1");
		if(b1.toggle(this,"bb1")) {
			
		}
		
		if(b.toggle(0)) {
			
		}
		//this needs to be placed last
		b.theme.run();
	};
	
	public void mousePressed() {

		//f.writeLine("hello");
	};

	public void keyPressed() {

	};

```

please make sure to download my latest release if you are going to try it.

and use `setPos(x,y)` to update positions.

---

<div class="post-metadata">

### Author: ![gyula](https://avatars.discourse-cdn.com/v4/letter/g/ccd318/32.png) [@gyula](https://discourse.processing.org/u/gyula)
#### Post date: [June 16, 2021, 2:46pm UTC](https://discourse.processing.org/t/change-size-of-g4p-controls/30722/7 "2021-06-16T14:46:06Z")

</div>

Sorry but is that a working example? I get a NullPointerException at:  
`b.runEmpty(mouseButton);`

---

<div class="post-metadata">

### Author: ![paulgoux](https://avatars.discourse-cdn.com/v4/letter/p/b9bd4f/32.png) [@paulgoux](https://discourse.processing.org/u/paulgoux)
#### Post date: [June 16, 2021, 2:49pm UTC](https://discourse.processing.org/t/change-size-of-g4p-controls/30722/8 "2021-06-16T14:49:54Z")

</div>

please redownload the lib I updated when I posted the example, and the error should be cleared.

---

<div class="post-metadata">

### Author: ![gyula](https://avatars.discourse-cdn.com/v4/letter/g/ccd318/32.png) [@gyula](https://discourse.processing.org/u/gyula)
#### Post date: [June 16, 2021, 2:54pm UTC](https://discourse.processing.org/t/change-size-of-g4p-controls/30722/9 "2021-06-16T14:54:45Z")

</div>

Redownloaded: still the same error.

---

<div class="post-metadata">

### Author: ![paulgoux](https://avatars.discourse-cdn.com/v4/letter/p/b9bd4f/32.png) [@paulgoux](https://discourse.processing.org/u/paulgoux)
#### Post date: [June 16, 2021, 3:01pm UTC](https://discourse.processing.org/t/change-size-of-g4p-controls/30722/10 "2021-06-16T15:01:46Z")

</div>

I’d forgotten to remove the docks try it now.

---

<div class="post-metadata">

### Author: ![paulgoux](https://avatars.discourse-cdn.com/v4/letter/p/b9bd4f/32.png) [@paulgoux](https://discourse.processing.org/u/paulgoux)
#### Post date: [June 16, 2021, 3:15pm UTC](https://discourse.processing.org/t/change-size-of-g4p-controls/30722/11 "2021-06-16T15:15:38Z")

</div>

and you should be able to set the width and height by simply doing;

```auto
// set width and height to appropriate vars
b1.w = width;
b1.h = height;

```

---

<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: [June 16, 2021, 3:22pm UTC](https://discourse.processing.org/t/change-size-of-g4p-controls/30722/12 "2021-06-16T15:22:29Z")

</div>

You can get rid of a control with  
`control.dispose();`  
and then recreate it with  
`control = new GButton(... );`  
but since you have a new object you will have to add the event handler again. It would not be too inefficient provided you only created the control when the finished size is reached i.e. not while the mouse is dragging through intermediate sizes.

All visible G4P controls have an off screen buffer for its visual appearance which is lazily updated when its state changes. This is CPU friendly at the cost of a little RAM.

---

<div class="post-metadata">

### Author: ![paulgoux](https://avatars.discourse-cdn.com/v4/letter/p/b9bd4f/32.png) [@paulgoux](https://discourse.processing.org/u/paulgoux)
#### Post date: [June 16, 2021, 8:42pm UTC](https://discourse.processing.org/t/change-size-of-g4p-controls/30722/13 "2021-06-16T20:42:59Z")

</div>

I see you make use of the textfield class in g4p did you also want example code for my textarea alternative

---

<div class="post-metadata">

### Author: ![gyula](https://avatars.discourse-cdn.com/v4/letter/g/ccd318/32.png) [@gyula](https://discourse.processing.org/u/gyula)
#### Post date: [June 17, 2021, 9:04am UTC](https://discourse.processing.org/t/change-size-of-g4p-controls/30722/14 "2021-06-17T09:04:04Z")

</div>

Could you start a new topic with lots of examples so that others can also notice your library? I for one would be happy to follow that new thread.

BTW this worked for me:

```auto
public void mouseDragged() {

  if ( b1.pos() ) { // another "hidden" method :)
    if ( !keyPressed ) {
      float x = b1.x + mouseX - pmouseX;
      float y = b1.y + mouseY - pmouseY;
      b1.setPos(x, y);
    } else {
      float w = b1.w + mouseX - pmouseX;
      float h = b1.h + mouseY - pmouseY;
      b1.w = w;
      b1.h = h;
    }
  }
}

```

---

<div class="post-metadata">

### Author: ![paulgoux](https://avatars.discourse-cdn.com/v4/letter/p/b9bd4f/32.png) [@paulgoux](https://discourse.processing.org/u/paulgoux)
#### Post date: [June 17, 2021, 5:08pm UTC](https://discourse.processing.org/t/change-size-of-g4p-controls/30722/15 "2021-06-17T17:08:46Z")

</div>

I shall when its done, i still have a bit of work finalising themes
