Projucer sets the first ComboBox index at 1?


#1

hey,
when I create a ComboBox with Projucer and fill in the list of items, Projucer will set the first elements index at 1.
This is problematic since I’m binding the selected item to a Value with referTo() in my ValueTree.
so why is projucer setting the first index at 1 and not 0?
and is there a workaround? f.e. how can I bind the Value with referTo() -1?
cheers!
EDIT: I confused tabbedComponent with ComboBox… sorry!

edit: I’m on OSX


#2

is this a bug?


#3

I can’t reproduce what you are describing, though I did the following:

  • I created a new GUI Application
  • I replaced the default generated MainComponent by a New GUI Component
  • I added a New Tabbed Component to that GUI Component (it contains 3 tabs by default)
  • I added DBG(tabbedComponent->getCurrentTabIndex()); to the resized() method of my New GUI Component
  • I built and launched the app: when I select the first tab and resize the application window, I get a bunch of 0 printed in the output of my IDE, i.e. the index of the first tab is 0.

I guess you need to share some code so we can understand what is happening in your case.


#4

thanks for the answer!
I’m sorry I ment ComboBox… I’ll fix the header.
so stupid sorry!

so when I initialize a ComboBox and add items thru the projucer gui builder I’m getting this in my generated code:
CB.reset (new ComboBox (“CB1”));
addAndMakeVisible (CB.get());
CB->setTooltip (TRANS(“CB 1”));
CB->setEditableText (false);
CB->setJustificationType (Justification::centredLeft);
CB->setTextWhenNothingSelected (String());
CB->setTextWhenNoChoicesAvailable (TRANS("(no choices)"));
CB->addItem (TRANS(“off”), 1);//??
CB->addItem (TRANS(“on”), 2);


#5

The number that comes after the item’s name is not the index, but the item’s ID.
The index works just fine, and is completely independent from the item ID.
The ID apparently has to be a positive number (I’ve experienced this in my projects as well).

There are two solutions to your issue: Either subtract one when using the value from your ValueTree (you could write a getter for it), or use something like:

CB->onChange = [this]() {
    myValue = CB->getSelectedItemIndex();
}

to always set the ValueTree value to the selection index.


#6

You can also find information about the ID starting at 1 in the documentation, I would always recommend to read the documentation of the class when asking yourself if a behaviour is intended or a bug. There you’ll find e.g.

int ComboBox::getSelectedId() const
noexcept
Returns the ID of the item that's currently shown in the box.

If no item is selected, or if the text is editable and the user has entered something which isn't one of the items in the list, then this will return 0.

https://docs.juce.com/master/classComboBox.html#ab6232527104faad901ba0fa1380cd8ec


#7

I see, thanks!