Components that figure out their own width


We regularly seem to have components that specify their own width which need to be layed out. and I end up with a function something like this:

        int getIdealWidth(int height) override 
            Font f;
            f.setHeight(height - 4.0f);
            return f.getStringWidth(name) + 8; 

The JUCE pattern I’m used to is one where the parent component mainly tells the child how big it’s going to be, or the child specifies a size.

In my app we always seem to have the parent specifying one dimension and the child component figuring out the size in the other dimension …

Is there a tidier way of doing this? Sometimes it’s way more complicated, like when we have something whose height depends on it’s width, and then discover that we need to inject a scrollbar with the total height, then have to iterate over all the components again…


It sounds like you’re positioning a variety of things relative to each other, in some kind of grid, list, or both?

Aside from the obvious ListBox controls, maybe FlexBox or Grid would do the trick?


Maybe I should try FlexBox again for some of this. Seems like it should be good for things like this?


But I don’t think I understand what happens if the component needs to change its fixed width if the height changes… maybe I need to give it a go.


Can’t you derive from ComponentListener and handle componentMovedOrResized() ?