Create and show a new component without being dependence

Hi folks,

I have a newbie question. I would like to create and show a child component on the fly (without being a dependence of the parent).
For example, call a method in the parent:

        void createButton() {
            TextButton button;
            button.setSize(100, 100);
            button.setTopLeftPosition(10, 10);

Is there any ‘legal’ way to do that?

I guess it’s not working because the parent needs to know what to do with it when resizes.

Any thoughts?
Thanks all!

That method isn’t going to work because you create the button as a local variable. The button is deleted at the end of the createButton method. (addAndMakeVisible doesn’t and can’t make a copy of the object, because Juce Component is not copyable.) If you want to create components dynamically, you need allocate them on the heap.

What are you actually trying to do? Is there a reason you could not store (smart) pointers to your buttons in an array or vector that is a member of the parent object?

Thanks @Xenakios, I’m trying different strategies to solve my problem, so I’m not tied to any particular method.
What I’m trying to do is to create a ‘dummy’ Component that can hold one or another subcomponent (Login component or Registration component).
I’m using the dummy component as a canvas to put within the login or the registration component.
Is there any other way to achieve what I’m trying to do?

The details depend on how exactly you want it all working, so it’s pretty hard to give any specific advice.

What I’m going to do is to create both sub components, and then show one or the other when I need them.
My first idea was to destroy and create them dinamically, but I think it’s not possible :frowning:

It’s possible but it does complicate things a bit.

1 Like