Placing Components on GUI when using Multiple classes

Firstly, I want to thank everyone who has helped me in the past, this resource is incredibly useful for people like me who are building their first JUCE project.

I have A class that overrides Component:
void OpenGLComponent::resized()
{
std::cout << “OpenGLComponent resized” << std::endl;
openGLComponent->setBounds(getWidth() / 2 - 200, getHeight() - 300, 400, 300);
openGLComponent->toFront(true);
openGLComponent->setVisible(true);
}

I am trying to make this component display on the main GUI which is handled in the Editor. I have of course called: addAndMakeVisible(openGLComponent); in the Editors constructor.

This is my resize in the Editor:

..../
openGLComponent.setBounds(getWidth() / 2 - 200, getHeight() - 300, 400, 300);
openGLComponent.toFront(true);
openGLComponent.setVisible(true);

I am Not sure how to strucutre the placement of the OpenGLComponent so it displays, What should I call and where? I have looked at the tutorials but the only deal with single class issues really. Many thanks,

ideally you should design your component hierarchy to not require toFront by simply adding them to their parents in the exact sequence in which stuff would be layered if it was a photoshop document. what you got so far should already paint the component. i could only imagine that either one of the components doesn’t paint more than full transparency yet, in which case you wouldn’t notice that it already works, or your setBounds calls go out of bounds. note that you always define bounds relative to its parent, rather than relative to the window with setBounds. also i wouldn’t advice mixing absolute pixel values with relative ones like that. meaning on the one hand using getWidth and getHeight but on the other hand subtracting constant values from it. because this will stop working the moment you try different window bounds. if possible, design stuff relative only. exception can be working with images. working with absolute values can also lead to things being resized out of bounds accidently

Thank you for your response I know the layout is poor I have been focussing on my logic and the layout is next. Yes the fact that it isnt even showing up is the confusing part to me, I shall test with some .setOpaque to see if its actually there but im not convinced it is…

Note that not the order you declare the members is responsible, but the order you call addAndMakeVisible. Last added appears on top.

1 Like

It is my last added component and still the same issue, many thanks

openGLComponent.setBounds(getWidth() / 2 - 200, getHeight() - 300, 400, 300);
std::cout << "openGLComponent bounds: " << openGLComponent.getBounds().toString() << std::endl;
openGLComponent.toFront(true);
openGLComponent.setVisible(true);
openGLComponent.setOpaque(true);

When I print the bounds of the openGLComponent, it returns: openGLComponent bounds: 800 1700 400 300. So clearly it ‘Exists’ as an initalised object, just not sure why I cant see it at all!

you don’t need to override OpenGLComponent::resized(), maybe that’s the problem.

I tried but that didnt work!

make sure that the function has the “override” specifier, in case you put the wrong name, for example resize instead of resized, because then it will never be called.

you can also try resizing just after adding the component, or try other simplified coordinates like 0,0, 100,100 to find out where the problem is.