menuBarComponent not showing up, begging for help!

The menu bar would not show up.

I have read every single possible thing on any possible thread or docs or source codes on this. I copied and recopied and written every single possible thing on the menu bar. I watched every single video on menu topics. Spent more than two days on it.

It just would not show up.

I apologize in advance for being so agitated. But if anyone can point me in the right direction or give any advise I would be extremely appreciative.

I should I use the “DocumentWindow::setMenuBarComponent” function? I have tried that in the document window, but to zero avail. That why the menuComponent is in the public namespace of MainMenu. And I haven’t seen everyone doing that.

Thhe code below is a modified version of a previous question answered. Or is the menuBar feature deprecated? If I were to make my own menu bar, I would have to make my own event system, which is why I haven’t tried.

/*
  ==============================================================================

    MainMenu.h
    Created: 13 Nov 2022 7:29:04pm
    Author:  18604

  ==============================================================================
*/

#pragma once

#include <JuceHeader.h>

//==============================================================================
/*
*/
class MainMenu  : public juce::Component,
    public juce::ApplicationCommandTarget,
    public juce::MenuBarModel
{
public:
    MainMenu() {
        // In your constructor, you should add any child components, and
        // initialise any special settings that your component needs.
        // Setup 
        menuComponent.reset(new juce::MenuBarComponent());
        addAndMakeVisible(menuComponent.get());

        setApplicationCommandManagerToWatch(&commandManager);
        commandManager.registerAllCommandsForTarget(this);

        // this ensures that commands invoked on the DemoRunner application are correctly forwarded to this demo
        commandManager.setFirstCommandTarget(this);

        // this lets the command manager use keypresses that arrive in our window to send out commands
        addKeyListener(commandManager.getKeyMappings());

        setWantsKeyboardFocus(true);

        //setSize(500, 500);
        #if JUCE_MAC
        MenuBarModel::setMacMainMenu(this);
        #endif
    }
    ~MainMenu() override {
        #if JUCE_MAC
        MenuBarModel::setMacMainMenu(nullptr);
        #endif
        commandManager.setFirstCommandTarget(nullptr);

    }


    void resized() override { menuComponent->setBounds(getLocalBounds()); }
    enum CommandIDs
    {
        newProject = 1,
        openProject,
        saveProject,
        importFile,
        exportFile,
        undo,
        redo,
        option1,
        option2
    };

    juce::StringArray getMenuBarNames() override
    {
        return { "File", "Edit", "Align" };
    }

    juce::ApplicationCommandTarget* getNextCommandTarget() override
    {
        return this; // findFirstTargetParentComponent()
    }

    juce::PopupMenu getMenuForIndex(int index, const juce::String& name) override
    {
        juce::PopupMenu menu;
        switch (index) {
        case 0: // File menu
            menu.addCommandItem(&commandManager, CommandIDs::newProject);
            menu.addCommandItem(&commandManager, CommandIDs::openProject);
            menu.addCommandItem(&commandManager, CommandIDs::saveProject);
            menu.addCommandItem(&commandManager, CommandIDs::importFile);
            menu.addCommandItem(&commandManager, CommandIDs::exportFile);
            break;
        case 1: // Edit menu
            menu.addCommandItem(&commandManager, CommandIDs::undo);
            menu.addCommandItem(&commandManager, CommandIDs::redo);
            break;
        case 2: // Align menu
            menu.addCommandItem(&commandManager, CommandIDs::option1);
            menu.addCommandItem(&commandManager, CommandIDs::option2);
            break;
        }
        return menu;
    }

    void menuItemSelected(int /*menuItemID*/, int /*topLevelMenuIndex*/) override {}

    void getAllCommands(juce::Array<juce::CommandID>& c) override
    {
        juce::Array<juce::CommandID> commands
        {
            CommandIDs::newProject,
            CommandIDs::openProject,
            CommandIDs::saveProject,
            CommandIDs::importFile,
            CommandIDs::exportFile,
            CommandIDs::undo,
            CommandIDs::redo,
            CommandIDs::option1,
            CommandIDs::option2
        };
        c.addArray(commands);
    }

    void getCommandInfo(juce::CommandID commandID, juce::ApplicationCommandInfo& result) override
    {
        switch (commandID)
        {
        case CommandIDs::newProject:
            result.setInfo("New", "TODO", "File", 0);
            result.addDefaultKeypress('n', juce::ModifierKeys::ctrlModifier);
            break;
        case CommandIDs::openProject:
            result.setInfo("Open", "TODO", "File", 0);
            result.addDefaultKeypress('o', juce::ModifierKeys::ctrlModifier);
            break;
        case CommandIDs::saveProject:
            result.setInfo("Save", "TODO", "File", 0);
            result.addDefaultKeypress('s', juce::ModifierKeys::ctrlModifier);
            break;
        case CommandIDs::importFile:
            result.setInfo("Import", "TODO", "File", 0);
            result.addDefaultKeypress('i', juce::ModifierKeys::ctrlModifier);
            break;
        case CommandIDs::exportFile:
            result.setInfo("Export", "TODO", "File", 0);
            result.addDefaultKeypress('e', juce::ModifierKeys::ctrlModifier);
            break;
        case CommandIDs::undo:
            result.setInfo("Undo", "TODO", "Edit", 0);
            result.addDefaultKeypress('z', juce::ModifierKeys::ctrlModifier);
            break;
        case CommandIDs::redo:
            result.setInfo("Redo", "TODO", "Edit", 0);
            result.addDefaultKeypress('y', juce::ModifierKeys::ctrlModifier);
            break;
        case CommandIDs::option1:
            result.setInfo("Option 1", "TODO", "Align", 0);
            result.addDefaultKeypress('g', juce::ModifierKeys::ctrlModifier);
            break;
        case CommandIDs::option2:
            result.setInfo("Option 2", "TODO", "Align", 0);
            result.addDefaultKeypress('w', juce::ModifierKeys::ctrlModifier);
            break;
        }
    }

    bool perform(const InvocationInfo& info) override
    {
        // todo: make commands perform actions
        return true;
    }
    std::unique_ptr<juce::MenuBarComponent> menuComponent;
private:
    #if JUCE_DEMO_RUNNER
    juce::ApplicationCommandManager& commandManager = getGlobalCommandManager();
    #else
    juce::ApplicationCommandManager commandManager;
    #endif
    JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (MainMenu)
};

#include "MainComponent.h"

//==============================================================================
MainComponent::MainComponent()
{
    // Initializations
    note_list.reset(new NotesList());
    note_editor.reset(new NotesEditor());
    menu.reset(new MainMenu());

    // Sizes

    setSize(600, 400);

    top_cord = getLookAndFeel().getDefaultMenuBarHeight();
    note_list->setBounds(0, top_cord, middle_cord, getHeight() - top_cord);
    note_editor->setBounds(middle_cord,top_cord,getWidth()-middle_cord, getHeight()-top_cord);

    // Visibles
    addAndMakeVisible(ball);
    addAndMakeVisible(note_list.get());
    addAndMakeVisible(note_editor.get());
    addAndMakeVisible(menu.get());
    
    // Settingss

    // Timer
    Timer::startTimerHz(60);
}

MainComponent::~MainComponent()
{
    note_list = nullptr;
    note_editor = nullptr;
    menu = nullptr;
}

//==============================================================================
void MainComponent::paint (juce::Graphics& g)
{
    // (Our component is opaque, so we must completely fill the background with a solid colour)
    /*g.fillAll (getLookAndFeel().findColour (juce::ResizableWindow::backgroundColourId));
    if (is_in_tree) {
        g.fillAll(juce::Colour(0xff7fff00));
    }
    else {
        g.fillAll(getLookAndFeel().findColour(juce::ResizableWindow::backgroundColourId));
    }
    g.setFont (juce::Font (16.0f));
    g.setColour (juce::Colours::white);
    g.drawText (currentSizeAsString, getLocalBounds(), juce::Justification::centred, true);*/
    //g.fillAll(getLookAndFeel().findColour(juce::ResizableWindow::backgroundColourId));
    g.setColour(juce::Colours::white);
    g.setFont(16.0f);
    g.drawFittedText("Where is the menu", getLocalBounds(), juce::Justification::centred, 1);
    
}

void MainComponent::resized()
{

    auto b = getLocalBounds();
    ball.setBounds(0, getHeight() - (0.1 * getHeight()), middle_cord, (0.1 * getHeight()));
    note_list->setBounds(0, top_cord, middle_cord, getHeight() - (0.1 * getHeight()));
    note_editor->setBounds(middle_cord, top_cord, getWidth() - middle_cord, getHeight());
//menu->setBounds(b.removeFromTop(getLookAndFeel().getDefaultMenuBarHeight()));

    menu->setBounds(b.removeFromTop(juce::LookAndFeel::getDefaultLookAndFeel()
        .getDefaultMenuBarHeight()));
    
}

void MainComponent::timerCallback() {
    ball.pos_x += ball.speed_x;
    ball.pos_y += ball.speed_y;

    if (ball.pos_x <= 0 || ball.pos_x >= middle_cord) {
        ball.speed_x *= -1;
    }
    if (ball.pos_y <= 0 || ball.pos_y >= (0.1 * getHeight())) {
        ball.speed_y *= -1;
    }

    ball.repaint();
}


void MainComponent::mouseEnter(const juce::MouseEvent& event)
{
    mousePosition = juce::String(event.getScreenPosition().getX())
        + " x " + juce::String(event.getScreenPosition().getX());
    this->is_in_tree = 1;
}
void MainComponent::mouseExit(const juce::MouseEvent& event)
{
    this->is_in_tree = 0;
}