Laying out implicitly sized children

Hi all,

I just started learning JUCE, and for the most part I am getting to grips with it pretty quickly as I find the framework to be quite intuitive.

Until now I have been setting the size and position of child components in the parent’s resized function as shown in the tutorials.

However, what if I want to use the natural size of a child? For example, say I have a listbox and I want to position it such that it can show all its items and no more.

I have searched around in the API, but so far have been unable to find a way to do the following:

  • How to get the natural size of an existing component such as a TextEditor.
  • How to calculate and return the natural size of my own components.

All hints are appreciated.

With kind regards,

Well, Components, don’t have the concept of “natural size” in the sense you intend that a

For some of the widgets, you could infer some of those information, but you probably need to code that yourself depending on the type of Component, e.g. for a TextEditor you could ask the size of the Font that it is rendered with and deduce something about its size depending on the text that it contains.
Similarly, you could do for a listbox that has a certain amount of rows.

The reason why there is no built-in feature for that is, in my opinion, due to the fact that the appearance of those Components greatly depends on the LookAndFeel that you assign to them for the rendering

The recommended way to handle this now is with the Flexbox/Grid classes, where you do give preferred sizes to the items. It’s basically the same way layout is done using best practices on the web now.

But if I understand things correctly, in the Flexbox and Grid cases you still specify the size yourself. The children of these layout classes don’t specify how big they want to be, unlike in the web case where you can set the size of a grid column to be ‘auto’, and the column will be as big as it needs to be for the elements in it.

yes, that’s true. You’d have to figure those sizes out yourself when setting up the flexbox

Ok, thank you. That is what I thought, but I wanted to check that I am not going to build something that is already supported.