Troubles showing a new component

Hello! Newbie here.
So, briefly I am trying to add a new component, having a lable inside, to my mainComponent class.

But no matter how much I try, when building It doesn’t appear on the main window! The sub-component gets shown when launched on its own (replacing mainComponent class with subComponent class on the main.cpp), but not when it is imported on the mainComponent.
Here is my code structure:
maincomponent.cpp:

#include "MainComponent.h" 

MainComponent::MainComponent()
{
    setSize (600, 400);
    addAndMakeVisible (subComponent);
}


void MainComponent::paint (juce::Graphics&)
{ }

void MainComponent::resized()
{
    subComponent.setBounds(100, 80, getWidth() - 110, 20);
}

subcomponent.h (Header only)

#pragma once

#include <juce_gui_extra/juce_gui_extra.h>

class subComponent  : public juce::Component
{
public:
    //==============================================================================
    subComponent() : updateMessage("Loading...")
    {
        setSize (200, 200);
        addAndMakeVisible(updater);
        updater.setFont(juce::Font(16.0f, juce::Font::bold));
        updater.setText(updateMessage, juce::dontSendNotification);
        updater.setColour(juce::Label::textColourId, juce::Colours::orange);
        updater.setJustificationType(juce::Justification::bottomLeft);
    }

    void paint (juce::Graphics& g) override {}

    void resized() override
    {
        updater.setBounds(100, 80, getWidth() - 110, 20);
    }

private:

    juce::Label updater;
    std::string updateMessage;
    JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (setupper)
};

Your MainComponent is setting subComponent to have a height of “20”. Your subComponent is setting its label to also have a height of “20”, but is placing the origin of the label at a Y coordinate of “80”, which is outside the bounds of the component, so the label isn’t shown.

You probably need to tweak one/both of your resized function implementations.

1 Like

It works! Thanks!