# Setting bounds of child window with respect to parent window

**URL:** <https://forum.juce.com/t/setting-bounds-of-child-window-with-respect-to-parent-window/14181>\
**Category:** General JUCE discussion\
**Created:** [January 16, 2015, 8:06pm UTC](https://forum.juce.com/t/setting-bounds-of-child-window-with-respect-to-parent-window/14181 "2015-01-16T20:06:08Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![anish\_sngh](https://avatars.discourse-cdn.com/v4/letter/a/e9c0ed/32.png) [@anish\_sngh](https://forum.juce.com/u/anish_sngh)\
**Post date:** [January 16, 2015, 8:06pm UTC](https://forum.juce.com/t/setting-bounds-of-child-window-with-respect-to-parent-window/14181/1 "2015-01-16T20:06:08Z")

</div>

+--------- ------------------+  
|&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; A&nbsp;&nbsp;&nbsp; |&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; |&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; |  
|---------+ B &nbsp; |&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; |  
|---------------+&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; |&nbsp; &nbsp;  
|&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; C &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; |  
+----------------------------+

As you can see there are three rectangles 'A', 'B' and 'C'.

'A' is the smallest rectangle. 'B' is also a rectangle which is enclosing 'A' and 'C' is also a rectangle which is enclosing 'B'

'C' is the main window which is resizable. I want to create 'B' and 'A' window as shown and even if the main window is resized the child windows should not move from top left position but can stretch.

I am new to GUI programming so need some help here. Below is my code which is doing something as below:

+--------- -----------------+  
| &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; |  
|&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp; &nbsp;&nbsp; &nbsp;&nbsp;&nbsp;&nbsp; +------------+  
|&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; | &nbsp; &nbsp; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; |  
+---------------------------+

Code below is creating the child window in bottom right which is what i don't want.

MainComponent.cpp

void MainContentComponent::resized()  
{  
&nbsp;&nbsp;&nbsp; if(draw\_component) {  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; draw\_component-\>setBounds(0, 0, getParentHeight()/4, getParentWidth()/4);  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; draw\_component-\>updateTransform();  
&nbsp;&nbsp; }  
}

In above setBounds where is 0, 0 co-ordinates in the rectangle?

&nbsp;

ChildComponent.cpp

void childComp::resized ()  
{  
&nbsp;&nbsp;&nbsp; setBounds ((int) (0.1f \* getParentWidth()), (int) (0.1f \* getParentHeight()), (int) (0.5f \* getParentWidth()), (int) (0.7f \* getParentHeight()));  
&nbsp;&nbsp;&nbsp; repaint();  
}

&nbsp;

Can you please give some code for doing what i intend to do as shown in the above diagrams with "A", "B" and "C" rectangle? I went through all the examples but couldn't infer how to do this.

&nbsp;

---

<div class="post-metadata">

**Author:** ![jules](https://avatars.discourse-cdn.com/v4/letter/j/41988e/32.png) [@jules](https://forum.juce.com/u/jules)\
**Post date:** [January 17, 2015, 11:45am UTC](https://forum.juce.com/t/setting-bounds-of-child-window-with-respect-to-parent-window/14181/2 "2015-01-17T11:45:15Z")

</div>

The resized() method is called when your component changes size, so obviously it's a really bad idea to change the component's size in there! Surprising that&nbsp;you didn't just end up with a recursive call + crash.

And what on earth is updateTransform()? If you're setting component&nbsp;transforms then you have to be prepared for the fact that the transform geometry is relative to the parent, so if the transformed&nbsp;comp has bounds that aren't at the origin, things can get a bit complicated. If you're still learning about setting component bounds, best to completely&nbsp;avoid transforms!

---

<div class="post-metadata">

**Author:** ![jimc](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.juce.com/jimc/32/16067_2.png) [@jimc](https://forum.juce.com/u/jimc)\
**Post date:** [January 17, 2015, 12:41pm UTC](https://forum.juce.com/t/setting-bounds-of-child-window-with-respect-to-parent-window/14181/3 "2015-01-17T12:41:14Z")

</div>

​It should be easy...

- 0,0 is the top left.&nbsp;
- Stick to having the parent component control it's children to start with. &nbsp;
- Call setBounds on the child from its parents resize method.&nbsp;

And it should all work out.

You can have a child change its own size, but if you are just starting out, I'd manage the size from the parent for now. &nbsp;Like jules says, you can get in a loop ...&nbsp;
