Keep position of draggable component after resize


I'm very new to JUCE and currently developing my very first Audio Plugin.

I implemented a component which is draggable, but every time I resize the main window the component jumps back to its start position. Is there anyway to get a notification that the window size is changing right now or to tell the ComponentDragger to keep the last position?


My guess would be that the parent component's resized() function is setting the bounds of that child component.

Thanks, I tried that, but then the component does not appear at all.

Maybe an important information: I only managed to make it work, when I initialized my own object in the header file. When I only declared it and initialized it in the cpp file, I got an "EXC_BAD_ACCESS Error" at the point where the bounds for this object were set in the resized() function. The bounds depend on some constants in the object class, that might be the reason for that.

Anyway, it worked to create the object and drag it, but as mentioned, the jump back when it gets resized is the problem now. And only setting the bounds in the child component class and not in the parent, let the object to not appear at all.

There's nothing wrong with setting the bounds in resized - that isn't a problem, but it is the reason the component is moving when the parent is resized (because it is being explicitly told to). If that's the only place you're setting the bounds of the object, then not setting the bounds here will of course mean that it never gets placed anywhere (and so you'd never see it).

You just need to make sure that the bounds are set somewhere (e.g. when the component is created). You might want to set the bounds in resized still (e.g. if you wanted to keep it aligned to the right edge) - you'd just need to make sure that you're setting the bounds that you want, and not just the original bounds!

Thank you. I understand that if I don't set the bounds at all, it will not be placed. I tried now to set the bounds just after the object is created and also in the object class itself. But not in the resized() function of the main component. The object is created, but doesn't appear on the screen, unfortunately.

So, just to make it clear: it is not that important to set bounds in the resized() function? I can do it anywhere else too?

Yeah, resized is just a fairly common place to put such a call, since often you'll want the child's bounds to update in relation to the parent (e.g. If the setBounds call includes some calculations involving things like 'getWidth', then obviously the child's bounds are dependent upon those of the parent, and would need to be refreshed accordingly).


My next guess would be that perhaps the above example is true of your case here; if the child's bounds calculations depend on the parent's bounds, then these will not be meaningful until the parents bounds have actually been set (e.g. getHeight will return 0 in the constructor). If you're doing something like 'child->setBounds(...)' in your parent object's constructor, make sure it's either using a fixed size, or comes after a call to setSize(). Just be aware that setSize will trigger resized() too, so make sure that resized doesn't try to dereference anything that hasn't been created yet!



Thanks a lot! I tried that, but it seems that my problem is somewhere else. The object is created, the bounds are set correctly, the paint function of the component is called, but I still can't see it....

Thank you anyway, learned something :)