# ComboBox: Handling item reselection

**URL:** <https://forum.juce.com/t/combobox-handling-item-reselection/30264>\
**Category:** General JUCE discussion\
**Created:** [November 9, 2018, 2:14pm UTC](https://forum.juce.com/t/combobox-handling-item-reselection/30264 "2018-11-09T14:14:30Z")\
**Posts on this page:** 11\
**Page:** 1

<div class="post-metadata">

**Author:** ![nschikora](https://avatars.discourse-cdn.com/v4/letter/n/9de0a6/32.png) [@nschikora](https://forum.juce.com/u/nschikora)\
**Post date:** [November 9, 2018, 2:14pm UTC](https://forum.juce.com/t/combobox-handling-item-reselection/30264/1 "2018-11-09T14:14:30Z")

</div>

What is the proper way of getting notified when the user selects the same item in a ComboBox that is currently selected?

What I tried so far with no success:  
Overriding `comboBoxChanged` or `valueChanged` or `handleAsyncUpdate` in a subclassed `ComboBox`. All of those methods get only called if the value actually changed.

What I tried with partial success:  
Passing a `PopupMenu::CustomCallback` that handles `menuItemTriggered()` to every `PopupMenu::Item` in the `ComboBox`. Problem here is that the callback in fact is called every time an item is selected but it has no knowledge of the actual item that was selected. Now asking the `ComboBox` which item is currently selected will still return the old value because it will only get updated after this callback.

From the description of `menuItemTriggered()` I would have assumed that always returning `true` from it will always force `ComboBox::comboBoxChanged()` to be called but that turned out to be a misassumption (Probably because another check for change is run after `exitModalState`):

> true if the itemID should be sent to the exitModalState method, or false if it should send 0, indicating no further action should be taken

Now I guess I could make the `PopupMenu::CustomCallback::menuItemTriggered()` smart enough to know which item was selected if the callback is called but that would IMO imply having a `PopupMenu::CustomCallback` for every item in the `ComboBox` which seems overly complicated to me and I fear I’m missing the easy way.

Any suggestions on how to tackle this?

---

<div class="post-metadata">

**Author:** ![refusesoftware](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.juce.com/refusesoftware/32/20561_2.png) [@refusesoftware](https://forum.juce.com/u/refusesoftware)\
**Post date:** [October 24, 2019, 6:32pm UTC](https://forum.juce.com/t/combobox-handling-item-reselection/30264/2 "2019-10-24T18:32:05Z")

</div>

I came up against this same problem today, when making a menu for plug-in presets.

Selecting a preset from the menu the first time would load the new parameter values just fine, but then selecting the same preset again would do nothing. This is a problem when a user modifies a parameter value in between! You want them to be able to revert to the stored preset values by selecting it again – but in its default behavior, ComboBox doesn’t do anything if a selection is the same as the previous one.

I looked into making the ComboBox an `AudioProcessorValueTreeState::Listener`, so it would be updated of changed parameter values, and thereby clear/reset its preset selection when that happened. However, for several reasons that I won’t delve into for now, that seemed like a bad idea.

In the end I figured out a stupid hack, which I’ll share here below. But I would appreciate any feedback from folks wiser in these matters…

In the Editor constructor, after `addAndMakeVisible(presetMenu)` and whatever else you might do there, add this:

```
presetMenu.onChange = [this]{
	if (presetMenu.getSelectedItemIndex() >= 0)
	{
		presetMenu.setTextWhenNothingSelected(presetMenu.getText());
		presetMenu.setSelectedId(0);
	}
};

```

Now whenever you make a selection from the ComboBox, it immediately resets itself to the “nothingSelected” state (i.e. index 0), but it changes the content of the “nothingSelected” text so that it _looks_ like your selection is still made.

---

<div class="post-metadata">

**Author:** ![stephenk](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.juce.com/stephenk/32/15198_2.png) [@stephenk](https://forum.juce.com/u/stephenk)\
**Post date:** [January 17, 2021, 6:44am UTC](https://forum.juce.com/t/combobox-handling-item-reselection/30264/3 "2021-01-17T06:44:20Z")

</div>

I have the same problem. I would like to be able to reselect the same item, and have it be processed as if it was selected for the first time. This hack doesn’t seem to work any longer…

It sure would be nice to add a method to ComboBox so you could specify this behavior, such as setCanReselectSameItem(bool state);

---

<div class="post-metadata">

**Author:** ![refusesoftware](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.juce.com/refusesoftware/32/20561_2.png) [@refusesoftware](https://forum.juce.com/u/refusesoftware)\
**Post date:** [January 17, 2021, 8:59pm UTC](https://forum.juce.com/t/combobox-handling-item-reselection/30264/4 "2021-01-17T20:59:51Z")

</div>

> [@stephenk](#):
>
> This hack doesn’t seem to work any longer…

Just checked with a fresh JUCE 6 build, and it’s still working here. What part of it isn’t working for you?

---

<div class="post-metadata">

**Author:** ![stephenk](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.juce.com/stephenk/32/15198_2.png) [@stephenk](https://forum.juce.com/u/stephenk)\
**Post date:** [January 17, 2021, 9:01pm UTC](https://forum.juce.com/t/combobox-handling-item-reselection/30264/5 "2021-01-17T21:01:21Z")

</div>

hmm… I must be doing something wrong. Let me look into it again…

---

<div class="post-metadata">

**Author:** ![stephenk](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.juce.com/stephenk/32/15198_2.png) [@stephenk](https://forum.juce.com/u/stephenk)\
**Post date:** [January 17, 2021, 10:17pm UTC](https://forum.juce.com/t/combobox-handling-item-reselection/30264/6 "2021-01-17T22:17:13Z")

</div>

OK, when I use this, after making my selection, the code is indeed processed, but the item text is grayed out and the item is not checked in the popup menu.

I assume the greyed out text is just a LookAndFeel issue which draws the textWhenNothingSelected in a different colour, but when I open the popup menu, nothing is checked as being selected. Maybe that’s a trade-off you decided was OK?

For example, I simply process my function first, then do what you say:

```auto

    guiScaleCB.onChange = [this]{
        if (guiScaleCB.getSelectedItemIndex() >= 0)
        {
            setGUIScaleFactor(guiScaleCB.getSelectedItemIndex());

            guiScaleCB.setTextWhenNothingSelected(guiScaleCB.getText());
            guiScaleCB.setSelectedId(0);
        }
    };

```

This is after selecting 150%:  
 ![Untitled-1](https://us1.discourse-cdn.com/flex026/uploads/juce/original/2X/2/2fbace2bd2dc10a7d778c0e743755f19c60af98b.png)

---

<div class="post-metadata">

**Author:** ![stephenk](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.juce.com/stephenk/32/15198_2.png) [@stephenk](https://forum.juce.com/u/stephenk)\
**Post date:** [January 17, 2021, 10:26pm UTC](https://forum.juce.com/t/combobox-handling-item-reselection/30264/7 "2021-01-17T22:26:41Z")

</div>

On another topic, are we “allowed” to modify JUCE source and publish an app with it?

I mean, fixing this issue is a one line tweak. Just comment out the test for inequality:

```auto
void ComboBox::setSelectedId (const int newItemId, const NotificationType notification)
{
    auto* item = getItemForId (newItemId);
    auto newItemText = item != nullptr ? item->text : String();

    //if (lastCurrentId != newItemId || label->getText() != newItemText)
    {
        label->setText (newItemText, dontSendNotification);
        lastCurrentId = newItemId;
        currentId = newItemId;

        repaint(); // for the benefit of the 'none selected' text

        sendChange (notification);
    }
}

```

Of course, you have to remember to modify this again every time a new JUCE version is released, but it’s better than hacks…

---

<div class="post-metadata">

**Author:** ![daniel](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.juce.com/daniel/32/790_2.png) [@daniel](https://forum.juce.com/u/daniel)\
**Post date:** [January 17, 2021, 10:41pm UTC](https://forum.juce.com/t/combobox-handling-item-reselection/30264/8 "2021-01-17T22:41:04Z")

</div>

You accomplish that with a fork, and that is absolutely leagl.

btw. another way to deal with that issue is to add your own lambdas to the PopupMenu of the ComboBox.

```auto
if (auto* menu = combo.getRootMenu())
    for (int i=0; i < presetNames.size(); ++i)
        menu->addItem (presetNames [i], [processor, i] { processor.setPreset (i); });

```

…untested…

---

<div class="post-metadata">

**Author:** ![refusesoftware](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.juce.com/refusesoftware/32/20561_2.png) [@refusesoftware](https://forum.juce.com/u/refusesoftware)\
**Post date:** [January 17, 2021, 11:14pm UTC](https://forum.juce.com/t/combobox-handling-item-reselection/30264/9 "2021-01-17T23:14:21Z")

</div>

> [@stephenk](#):
>
> I assume the greyed out text is just a LookAndFeel issue which draws the textWhenNothingSelected in a different colour, but when I open the popup menu, nothing is checked as being selected. Maybe that’s a trade-off you decided was OK?

Correct, in the case I was using this for, I didn’t care about that trade-off.

Yes, the greyed out text can be avoided by changing the LookAndFeel method, I overrode the `drawComboBoxTextWhenNothingSelected` to avoid that.

Drawing the checkmark is also handled in a LookAndFeel method (`drawPopupMenuItem`), but when you call `setSelectedId(0)`, that means the ComboBox no longer “knows” which item was selected… so it has no way to pass that on to the LookAndFeel.

---

<div class="post-metadata">

**Author:** ![stephenk](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.juce.com/stephenk/32/15198_2.png) [@stephenk](https://forum.juce.com/u/stephenk)\
**Post date:** [January 18, 2021, 12:16am UTC](https://forum.juce.com/t/combobox-handling-item-reselection/30264/10 "2021-01-18T00:16:54Z")

</div>

Please vote!

> [@FR: Option to allow ComboBox to reselect the same item](https://forum.juce.com/t/fr-option-to-allow-combobox-to-reselect-the-same-item/43879):
>
> Presently, it is impossible to reselect the same item from a ComboBox - it is filtered out. However, this can be a useful expected behavior, as in the case of reselecting a preset in order to blow out any edits. This has been discussed in multiple threads here. Therefore, it seems it would be really easy to add this as an option. It’s basically a one line edit to the .cpp file and the inclusion of a bool variable. Something like this: ComboBox.h: public: void setCanReselectSameItem(const b…

---

<div class="post-metadata">

**Author:** ![refusesoftware](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.juce.com/refusesoftware/32/20561_2.png) [@refusesoftware](https://forum.juce.com/u/refusesoftware)\
**Post date:** [January 18, 2021, 12:52am UTC](https://forum.juce.com/t/combobox-handling-item-reselection/30264/11 "2021-01-18T00:52:21Z")

</div>

> [@daniel](#):
>
> btw. another way to deal with that issue is to add your own lambdas to the PopupMenu of the ComboBox.
> 
> ```auto
> if (auto* menu = combo.getRootMenu())
> for (int i=0; i < presetNames.size(); ++i)
> menu->addItem (presetNames [i], [processor, i] { processor.setPreset (i); });
> 
> ```
> 
> …untested…

I like this idea, and just tried implementing it, but it’s not working right off the bat. It **does** allow for the same item to be reselected, so it’s good on that front.

However, the ComboBox now seems unable to properly keep track of the items in the PopupMenu. At first, no checkmark is shown in the list, and then as soon as I select an item, a checkmark appears next to **every** item in the list. Also, the ComboBox’s text display (its Label) only shows the name of the first item, no matter which item is selected.

Note that the `ComboBox::addItem` method includes a `newItemId` argument, and that gets passed to the PopupMenu via [`PopupMenu::addItem` [4/6]](https://docs.juce.com/master/classPopupMenu.html#a48da0870a15df436dcf3596f6fcc56d0). But here, where you’re using [`PopupMenu::addItem` [2/6]](https://docs.juce.com/master/classPopupMenu.html#aef24f72b91ab31133411dc867440931b) instead, it doesn’t take an `Id` argument, and I’m not clear yet how to work around that. (Unfortunately there’s not a version of `PopupMenu::addItem` that takes **both** an Id argument and a std::function argument.)
