How to recognize which button was clicked in Property Pane of JuceDemo example?


#1

In this example when i clicked button, it only show one alertwindow.So i want When i click button 2,I want to show a button 2 and i will be show alertWindow ("This is button 2 ")?
Can anyone help me,please?
Thanks all.


#2

Pass the components propertyName into the message:

Current:
AlertWindow::showMessageBoxAsync (AlertWindow::InfoIcon, “Action Button Pressed”, “Pressing this type of property component can trigger an action such as showing an alert window!”);

Something like:
AlertWindow::showMessageBoxAsync (AlertWindow::InfoIcon, “Action Button Pressed”, "This is " + propertyName);


#3

Thank you but it does not work.


#4

Create a new GUI Project, and copy / paste these two.

MainComponent.h

#ifndef MAINCOMPONENT_H_INCLUDED
#define MAINCOMPONENT_H_INCLUDED

#include "../JuceLibraryCode/JuceHeader.h"


class MainContentComponent   : public Component, public ButtonListener // Note the ButtonListener
{
public:
    MainContentComponent();
    ~MainContentComponent();

    void paint (Graphics&);
    void resized();

    /* We need this so we can know the button was clicked. Note also that we have 'public ButtonListener' above as well. **/
    void buttonClicked (Button* buttonThatWasClicked) override;

    /* This sets up a couple of TextButtons **/
    ScopedPointer<TextButton> button1, button2;
    
private:
    JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (MainContentComponent)
};

#endif  // MAINCOMPONENT_H_INCLUDED

MainComponent.cpp

#include "MainComponent.h"

    MainContentComponent::MainContentComponent()
    {
        setSize (600, 400);

        button1 = new TextButton("button1"); // Set the name of the button
    addAndMakeVisible(button1); // add the button
    button1->setComponentID("button1"); // set Component ID
    button1->setButtonText("Button #1"); // Set the buttons text
    button1->setBounds(20, 20, 100, 20); // Set the location of the button
    button1->addListener(this); // So we know when it is pressed.
    
    // Rinse, repeat
    button2 = new TextButton("button2");
    addAndMakeVisible(button2);
    button2->setComponentID("button2");
    button2->setButtonText("Button #2");
    button2->setBounds(20, 50, 100, 20);
    button2->addListener(this);
}

MainContentComponent::~MainContentComponent()
{
}

void MainContentComponent::paint (Graphics& g)
{
    g.fillAll (Colour (0xff001F36));
}

void MainContentComponent::resized()
{
}

/* This occurs when the button is clicked. Because we added button1->addListener(this) and button2->addListener(this) in the constructor.**/
void MainContentComponent::buttonClicked (Button* buttonThatWasClicked)
{
    if (buttonThatWasClicked == button1 || buttonThatWasClicked == button2)
    {
        AlertWindow::showMessageBox(AlertWindow::InfoIcon, "Button Clicked", "You clicked: " + buttonThatWasClicked->getComponentID() + " Which has text of: " + buttonThatWasClicked->getButtonText(), "OK");   
    }
}

#5

I knew it and i am creating example like Property Demo which use propertyPanel.addSection (“Buttons & Toggles”, createButtons (3)) or something same like that.My problem is that not recognize exactly which array of button in function creatButtons().
Btw,thank you for your enthusiasm and helping me.


#6

This should create the buttons, no?

static Array<PropertyComponent*> createButtons (int howMany)
{
    Array<PropertyComponent*> comps;

    for (int i = 0; i < howMany; ++i)
        comps.add (new DemoButtonPropertyComponent ("Button " + String (i + 1)));

    for (int i = 0; i < howMany; ++i)
        comps.add (new BooleanPropertyComponent (Value (Random::getSystemRandom().nextBool()), "Toggle " + String (i + 1), "Description of toggleable thing"));

    return comps;
}

And this should occur when you click the button:

void buttonClicked() override
{
    ++counter;
    AlertWindow::showMessageBoxAsync (AlertWindow::InfoIcon, "Action Button Pressed",
                                      "Pressing this type of property component can trigger an action such as showing an alert window!");
    refresh();
}

Or have I misunderstood completely?


#7

Sorry,i am not good at english.
Simple, i click button 1,it will show “this is button 1 and it will do function 1” or i click button 2,it will show “this is button 2” and it will do function 2.It is my purpose.


#8

You could do the above for message.

And do a if or switch on the componentID for laughing different functions?