Hi folks, just getting going on Juce and it’s been a loooong time since I’ve done much C++ so definitely rusty. I’ve made a little app with the projucer, so it has the main.cpp that comes out of the box for a GUI app. I’m trying to get button listeners going according to the ListenersAndBroadcastersTutorial, but for the life of me I can’t figure out why my version of this doesn’t have the listener method fire. (or at least if it is, it’s not doing anything!) If anyone can tell me what I’m doing wrong that would be luverly…
Here’s my header file:
#pragma once
#include <JuceHeader.h>
//==============================================================================
/*
This component lives inside our window, and this is where you should put all
your controls and content.
*/
class MainComponent : public Component, public Button::Listener
{
public:
//==============================================================================
MainComponent(){
addAndMakeVisible ( btn );
btn.setButtonText ("Click me");
addAndMakeVisible (label);
label.setColour (Label::backgroundColourId, Colours::black);
label.setColour (Label::textColourId, Colours::white);
label.setJustificationType (Justification::centred);
label.setText ("waiting...", dontSendNotification);
setSize (600, 400);
};
//==============================================================================
void resized() override {
btn.setBounds (10, 10, getWidth() - 20, 40);
label.setBounds (10, 60, getWidth() - 20, 40);
}
// our listener callback, overrides method in the Button::Listener base class
// void buttonClicked (Button*) override;
void buttonClicked (Button* button) override {
// check button pointer and our member button to see if address matches
label.setText ("clicked!", dontSendNotification);
if (button == &btn){
// tell label component not to broadcast to *its* listeners
label.setText ("clicked!", dontSendNotification);
}
}
private:
//==============================================================================
// Your private member variables go here...
TextButton btn;
Label label;
JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (MainComponent)
};
And here’s the main from the projucer:
#include <JuceHeader.h>
#include "MainComponent.h"
//==============================================================================
class S7JuceApplication : public juce::JUCEApplication
{
public:
//==============================================================================
S7JuceApplication() {}
const juce::String getApplicationName() override { return ProjectInfo::projectName; }
const juce::String getApplicationVersion() override { return ProjectInfo::versionString; }
bool moreThanOneInstanceAllowed() override { return true; }
//==============================================================================
void initialise (const juce::String& commandLine) override
{
// This method is where you should put your application's initialisation code..
mainWindow.reset (new MainWindow (getApplicationName()));
}
void shutdown() override
{
// Add your application's shutdown code here..
mainWindow = nullptr; // (deletes our window)
}
//==============================================================================
void systemRequestedQuit() override
{
// This is called when the app is being asked to quit: you can ignore this
// request and let the app carry on running, or call quit() to allow the app to close.
quit();
}
void anotherInstanceStarted (const juce::String& commandLine) override
{
// When another instance of the app is launched while this one is running,
// this method is invoked, and the commandLine parameter tells you what
// the other instance's command-line arguments were.
}
//==============================================================================
/*
This class implements the desktop window that contains an instance of
our MainComponent class.
*/
class MainWindow : public juce::DocumentWindow
{
public:
MainWindow (juce::String name)
: DocumentWindow (name,
juce::Desktop::getInstance().getDefaultLookAndFeel()
.findColour (juce::ResizableWindow::backgroundColourId),
DocumentWindow::allButtons)
{
setUsingNativeTitleBar (true);
setContentOwned (new MainComponent(), true);
#if JUCE_IOS || JUCE_ANDROID
setFullScreen (true);
#else
setResizable (true, true);
centreWithSize (getWidth(), getHeight());
#endif
setVisible (true);
}
void closeButtonPressed() override
{
// This is called when the user tries to close this window. Here, we'll just
// ask the app to quit when this happens, but you can change this to do
// whatever you need.
JUCEApplication::getInstance()->systemRequestedQuit();
}
/* Note: Be careful if you override any DocumentWindow methods - the base
class uses a lot of them, so by overriding you might break its functionality.
It's best to do all your work in your content component instead, but if
you really have to override any DocumentWindow methods, make sure your
subclass also calls the superclass's method.
*/
private:
JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (MainWindow)
};
private:
std::unique_ptr<MainWindow> mainWindow;
};
//==============================================================================
// This macro generates the main() routine that launches the app.
START_JUCE_APPLICATION (S7JuceApplication)