Toolbar button does not show

Hello everyone,

I’ve been struggling to build a simple toolbar. The toolbar button does not show. I’ve been able to get my toolbar to show, but not the buttons. I’ve combed the forum and read various examples out there and went through the demo, yet I’m missing something. Here’s what the code looks like.

MainToolBarItemFactory.h

#pragma once

#include <JuceHeader.h>

//==============================================================================
/*
*/
class MainToolBarItemFactory : public juce::ToolbarItemFactory
{
public:
MainToolBarItemFactory();
~MainToolBarItemFactory() override;

void getAllToolbarItemIds(juce::Array <int>& ids);
void getDefaultItemSet(juce::Array <int>& ids);
juce::ToolbarItemComponent* createItem(int itemId);

private:
juce::ToolbarButton* button1;
JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (MainToolBarItemFactory)
};

MainToolBarITemFactory.cpp

#include <JuceHeader.h>
#include “MainToolBarItemFactory.h”
#include “BinaryData.h”

//==============================================================================
MainToolBarItemFactory::MainToolBarItemFactory() : button1(nullptr)
{

}

MainToolBarItemFactory::~MainToolBarItemFactory()
{
}

enum MainFactoryItems
{
New = 1,
Settings = 2
};

void MainToolBarItemFactory::getAllToolbarItemIds(juce::Array & ids)
{
ids.add (New);
ids.add(Settings);
}

void MainToolBarItemFactory::getDefaultItemSet(juce::Array & ids)
{
ids.add(New);
ids.add(Settings);
}

/juce::ToolbarButton MainToolBarItemFactory::createButtonFromImage(const int itemId, const juce::String& buttonName)
{
buttonImage = juce::Drawable::createFromImageData(BinaryData::Button_svg, BinaryData::Button_svgSize);
button1 = new juce::ToolbarButton(itemId, buttonName, juce::Drawable::createFromImageData(BinaryData::Button_svg, BinaryData::Button_svgSize), {});

return button1;

}*/

juce::ToolbarItemComponent* MainToolBarItemFactory::createItem(int itemId)
{
switch (itemId)
{
case New:
{
button1 = new juce::ToolbarButton(itemId, “New”, juce::Drawable::createFromImageData(BinaryData::newHomeButton_png, BinaryData::newHomeButton_pngSize), {});
return button1;
}
case Settings:
{
return nullptr;
}

default:
    break;
}
return nullptr;

}

MainToolBar.h

#pragma once

#include <JuceHeader.h>

#include “MainToolBarItemFactory.h”

//==============================================================================
/*
*/
class MainToolBar : public juce::Toolbar
{
public:
MainToolBar();
~MainToolBar() override;

void paint (juce::Graphics&) override;
void resized() override;

private:
MainToolBarItemFactory factory;
JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (MainToolBar)
};

MainToolBar.cpp

#include <JuceHeader.h>
#include “MainToolBar.h”
#include “MainToolBarItemFactory.h”

//==============================================================================
MainToolBar::MainToolBar()
{

addDefaultItems(factory);

}

MainToolBar::~MainToolBar()
{

}

void MainToolBar::paint (juce::Graphics& g)
{
g.fillAll(juce::Colours::white);
}

void MainToolBar::resized()
{

}

MainComponent.h

#pragma once

#include <JuceHeader.h>
#include “MainToolBar.h”

//==============================================================================
class MainComponent : public juce::Component
{
public:
//==============================================================================
MainComponent();
~MainComponent() override;

//==============================================================================
void paint (juce::Graphics&) override;
void resized() override;

private:
std::unique_ptr mainToolBar = std::make_unique();
JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (MainComponent)
};

MainComponent.cpp

#include “MainComponent.h”
#include “MainToolBar.h”

//==============================================================================
MainComponent::MainComponent()
{
addAndMakeVisible(*mainToolBar);
setSize(800, 600);
}

MainComponent::~MainComponent()
{
}

//==============================================================================
void MainComponent::paint (juce::Graphics& g)
{

}

void MainComponent::resized()
{
mainToolBar->setBounds(0, 0, getWidth(), 20);
}

All this code complies and generates a grey background, a white toolbar, and no buttons.
My thoughts on why this is happening? Other code examples I’ve seen have the listener added, but adding that to make the button visible doesn’t seem like the answer, especially when considering the Demo Code. My other thought is I need to make the button visible as a child component of the toolbar, but that doesn’t make sense because I thought that is what “addDefaultItems” was for? I have tried both, even though I question the logic behind was not able to implement either feature in a way that made the Toolbar Button visible.

If you can’t tell, I’m pretty new to C++, so I’m still wrapping my head around a lot of the functionality and features of the language. I hoping some experts can point me in the right direction. Any and all help is greatly appreciated. Thanks.

I figured it out!!!..my code was all wrong, but I see my errors and I was able to produce items on the toolbar.