How do I get a notification when a selection has been made in ChoicePropertyComponent()?


#1

Hi

How do I get  a notification when a selection has been made in ChoicePropertyComponent()?

I tried overloading the setIndex(), but that does not seem to work.

void MyDropdownChoice::setIndex (int newIndex)
{
    std::cout << "An index choice was made. \n";
}

I have inherited 'MyDropdownChoice' from 'ChoicePropertyComponent'.

class MyDropdownChoice    : public ChoicePropertyComponent
{
public:
  MyDropdownChoice (const Value &valueToControl, const String &propertyName, const StringArray &choices, const Array< var > &correspondingValues);
 
  void setIndex (int newIndex);
private:
    JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (MyDropdownChoice)
};

Any ideas of what I'm doing wrong and/or where I might be able to read up on this?

 

 Thanks

   Henry


#2

Looking at the code for ChoicePropertyComponent, the setIndex() function is only called if it isn't linked to a Value.

If you want your subclass to be responsible for applying the choice, then initialise it using the constructor that only takes a name (and populate the choices array yourself).

If you want to use the Value approach, you can get notified of the user's choice by simply registering as a listener to the Value that is being used.

I guess this is technically slightly different though, since you would be notified even if the value happened to change from elsewhere (i.e. the change isn't necessarily a guarantee that the user had actually chosen from the combobox). If you really needed this guarantee, you'd have to use the non-Value approach (as it is right now). To do so with the Value approach would require either a new callback, or the ComboBoxListener inheritence changed to protected so that you can override comboBoxChanged() and still call the version in the base class.

For most cases though, I'd wager that the Value::Listener callback would be sufficient!


#3

Thanks for the help.

It was the simple listener I was looking for, and I now have a working example :-).