I recently was searching for a similar answer: how to have a second window call member functions in the MainWindow and MainComponent (which is one way of describing what you want to do).
I was graciously helped by my friend Shane @getdunne with a couple of possibilities; here’s what’s working well for me now: storing a reference to the MainComponent in the second window’s component; you can then call member functions of the MainComponent.
I’m new to JUCE, I may not have all the terminology correct, but let me try to share the relevant pieces of code as examples. Ignore my function names, just look at what it does.
When you open/create the second window from the MainComponent (my main component is named MidiDemo, the content component of the second window is named MessagesWindowComponent and it is installed into a MessagesWindow DocumentWindow), pass a reference to *this:
MessagesWindow(MidiDemo& mainRef, const String& name, Colour backgroundColour, int buttonsNeeded);
void MidiDemo::showMessagesWindow ()
{
if (!messagesWindow)
{
messagesWindow.reset(new MessagesWindow(*this,
"Messages",
Desktop::getInstance().getDefaultLookAndFeel().
findColour(ResizableWindow::backgroundColourId),
DocumentWindow::allButtons));
}
messagesWindow->setVisible(true);
}
(I’m not killing the window on close, just hiding it; hence testing for the pointer and simply showing it if it exists.)
When the second window creates its content Component, pass it along:
MessagesWindowComponent::MessagesWindowComponent (MidiDemo& mainRef)
: mainComponent(mainRef)
{
[…]
}
In the second window component (MessagesWindowComponent), store the reference:
private:
MidiDemo& mainComponent;
Now, any button, slider etc. can call functions in the MainComponent, ie. say you have a button in the secondary component:
midiFilterClockButton.onClick = [this]
{
// call a member function of the MainComponent
mainComponent.someFunction();
};
Also, although I am not trying this right now, it seems if you have the pointer to the MainComponent, you could then add listeners and do things that way.
Sorry if that’s very muddled, without uploading the entire project, I hope it helps.