Load new component


#1

I have a application with a Maincomponent, which has some sub componenets on it.
I have another component I created using JUCER, so it has its own .h and .cpp files.
Now on my maincomponent I have a button that when clicked I want to destroy all the subcomponents on the main component windows and the load the component I create with JUCER.
I am able to destroy the existing components on the main component window, but I can’t get to load the other component?

can anyone help me?


#2

When you say “I can’t get to load the other component”, what exactly does that mean?


#3

well I used the startup files provided by Haydxn(I think).
So I have a MainAppWindow.cpp and .h,
I have a MainComponent.cpp and .h,
then I have created a mycomponent.cpp and .h using JUCER.

Currently on the MainComponent I have 3 texeditors and a button.
What I would like to do it when I click the button I want to remove all 4 thess components and load mycomponent.
Now I am able remove the 4 components when I click the button, that was easy, but I don’t know how to load that new component.
It has a class structure define fairly similar to MainComponent.


#4

Well, it’s just a Component, so you just have to create one, call addAndMakeVisible(newComponent), and set the component size and location with setBounds(x,y,width,height).


#5

If you want to ‘load’ the component, you just add it as you would any other component. You know how to add text editors and buttons to the MainComponent, yes? Well, you just need to do the same with a mycomponent instead.

[1] First you need to make sure that mycomponent.h is included in MainComponent.h (the main component needs to know what mycomponent is in order to create one in itself). You do that simply with:

at the top of MainComponent.h. Note: That’s the quickest way, ideally you’d use a forward declaration in the .h file, and put the include in the .cpp file - but for ease (for now at least) you can do it in the .h file.

[2] You want to give your MainComponent a pointer to a mycomponent object. I’m assuming your spelling of mycomponent isn’t using any capital letters - you might want to (for consistency) change it to MyComponent, or it may already be like that - i’m just going from your last post.

class MainComponent : public Component ... etc.... { private: ... MyComponent* mycomp; ... }
This gives you somewhere to store the component.

[3] You then simply create the component and store a pointer to it in that variable, and position it in the same way as you’d position the other components. I.e., when you want to create the component, you’d have a line like this:

mycomp = new MyComponent (/*params*/);
addAndMakeVisible (mycomp);

Be aware though that it is probably better to keep all your MainComponent subcomponents in an enveloping component. The reason? Well, consider your MainComponent::resized() function. You’ve got your subcomponents arranged there, but you don’t want them to be positioned when you’ve cleared them away… you’d end up with something like:

MainComponent::resized ()
{
   if (mycompVisible)
   {
      mycomp->setBounds (....);
   }
   else
   {
      .... position the rest
   }
};

You might instead have your subcomponents inside another component - lets say ‘mySubComponentsComponent’. Then you’d just need to position that on MainComponent, and swap it for MyComponent when you need to. That way, you can just have one pointer (e.g. contentComponent), and your resized function will position that - whatever it is. When you initialise your class, you’d create mySubComponentsComponent and store it in contentComponent. Resized would position that (E.g. setBounds (0,0,getWidth(),getHeight());). When you clear your subcomponents, you can remove and delete mySubComponentsComponent, and then add MyComponent by storing its pointer to contentComponent. Resized will then automatically apply to that instead, as its using the same pointer. You might want to ensure a check that the pointer is valid in the resized function.


#6