What is the recommended way to place components onto a window



All of my windows so far have used the method which is now deprecated in v5, where you add a component to the parent by name, then size and place them in reference to each other with a string:

addChildAndSetId( &stuff, "stuff" );
blah.setBounds( "parent.top + 5, blah.left + 2, parent.right - 20, blah.right" );

I need to create a few more windows, and would like to use the new recommended way of placing and sizing components. I know I can give absolute sizes and place them exactly where I want – but is there another recommended way I should be looking into?


I’m a fan of laying out components by subdividing rectangles. In your parent component’s resized() method you can use getLocalBounds() to get the bounding rectangle and then use the removeFrom...() methods to return a section of the rectangle which you can pass to your child component’s setBounds() method. There’s a good tutorial covering this here.

There’s also the FlexBox classes for more responsive layouts. Check out the FlexBox demo in the JUCE Demo for a simple example of using it.