# Truncate combobox text without changing the selectedID

**URL:** <https://forum.juce.com/t/truncate-combobox-text-without-changing-the-selectedid/16778>\
**Category:** General JUCE discussion\
**Created:** [February 24, 2016, 1:10pm UTC](https://forum.juce.com/t/truncate-combobox-text-without-changing-the-selectedid/16778 "2016-02-24T13:10:42Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![ehsanen](https://avatars.discourse-cdn.com/v4/letter/e/f07891/32.png) [@ehsanen](https://forum.juce.com/u/ehsanen)\
**Post date:** [February 24, 2016, 1:10pm UTC](https://forum.juce.com/t/truncate-combobox-text-without-changing-the-selectedid/16778/1 "2016-02-24T13:10:42Z")

</div>

Hi,

I have a combobox in my plugin which includes items with long names. I want to truncate the text after user has chosen an item. In other words, when the user selects an item from the popup menu I want to display the truncated version of that text in the comobox's textbox. I have a lookandfeel class and implemented "drawComboBox" function in it. At the end of this function I added the truncating logic. The problem is whenever I call setText on the combobox it will reset the selectedID to 0. Based on juce\_Combobox.h&nbsp;I already know why this is happening:

> &nbsp; &nbsp; &nbsp;&nbsp;
> 
> &nbsp; &nbsp;&nbsp;/\*\* Sets the contents of the combo-box's text field.
> 
> &nbsp; &nbsp; &nbsp; &nbsp; The text passed-in will be set as the current text regardless of whether
> 
> &nbsp; &nbsp; &nbsp; &nbsp; it is one of the items in the list. If the current text isn't one of the
> 
> &nbsp; &nbsp; &nbsp; &nbsp; items, then getSelectedId() will return -1, otherwise it wil return
> 
> &nbsp; &nbsp; &nbsp; &nbsp; the approriate ID.
> 
> &nbsp; &nbsp; &nbsp; &nbsp; @param newText&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; the text to select
> 
> &nbsp; &nbsp; &nbsp; &nbsp; @param notification &nbsp; &nbsp; determines the type of change notification that will
> 
> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; be sent to listeners if the text changes
> 
> &nbsp; &nbsp; &nbsp; &nbsp; @see getText
> 
> &nbsp; &nbsp; \*/
> 
> &nbsp; &nbsp; void setText (const String& newText,
> 
> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; NotificationType notification = sendNotificationAsync);

my question is, is it at all possible to do what I want to do? If yes, how?

&nbsp;

Thanks&nbsp;

---

<div class="post-metadata">

**Author:** ![yfede](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.juce.com/yfede/32/506_2.png) [@yfede](https://forum.juce.com/u/yfede)\
**Post date:** [February 24, 2016, 2:26pm UTC](https://forum.juce.com/t/truncate-combobox-text-without-changing-the-selectedid/16778/2 "2016-02-24T14:26:00Z")

</div>

Well, in the exact terms that you propose I don't think it is possible, however you could kinda obtain the same effect by mean of the ComboBox::addItemsToMenu () virtual method.

Essentialy, that method is there to let derived classes customize the entries added to the popup menu when it gets displayed, so, for your case, you should:

1) Use the "short" text for each item added to the combo with addItem(), because that's the text that will be displayed in the combo when "closed", and

2) Override the addItemsToMenu() method in your derived ComboBox class, so that it adds the "long" text for each entry in the menu instead of the "short" text.

Naturally, in order to do so the addItemsToMenu() method should know how to map each combo entry (using either its index, id or short text) to the long text to be displayed in the popup menu.&nbsp;

---

<div class="post-metadata">

**Author:** ![ehsanen](https://avatars.discourse-cdn.com/v4/letter/e/f07891/32.png) [@ehsanen](https://forum.juce.com/u/ehsanen)\
**Post date:** [February 24, 2016, 4:23pm UTC](https://forum.juce.com/t/truncate-combobox-text-without-changing-the-selectedid/16778/3 "2016-02-24T16:23:57Z")

</div>

Thanks, that solves the problem.

But that would be nicer if the Combobox allowed editing its label without changing the selectedID.

---

<div class="post-metadata">

**Author:** ![yfede](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.juce.com/yfede/32/506_2.png) [@yfede](https://forum.juce.com/u/yfede)\
**Post date:** [February 24, 2016, 4:44pm UTC](https://forum.juce.com/t/truncate-combobox-text-without-changing-the-selectedid/16778/4 "2016-02-24T16:44:37Z")

</div>

I'm not sure I agree completely: what should the behaviour be if, after setting the id for "Entry A", the label text is changed to "Entry B"? Having the id for A but the text for B sure would cause confusion and I'm pretty sure jules wouldn't follow this road.

However, it could be asked to the JUCE team what they think about adding an optional parameter to&nbsp;addItem(), in which to specify the text to be used in the popup menu (if different from the one in the label)

---

<div class="post-metadata">

**Author:** ![bigtick](https://avatars.discourse-cdn.com/v4/letter/b/5e9695/32.png) [@bigtick](https://forum.juce.com/u/bigtick)\
**Post date:** [July 4, 2021, 7:22pm UTC](https://forum.juce.com/t/truncate-combobox-text-without-changing-the-selectedid/16778/5 "2021-07-04T19:22:45Z")

</div>

I want to resurrect this request !  
Here is another use case, I need to display a dropdown with a list of presets. I want the displayed text when the menu is closed to be the preset name, but I also want the items in the menu to be prefixed by the preset number.

Right now I don’t think there’s any easy way to do that.

---

<div class="post-metadata">

**Author:** ![aguy-PG](https://avatars.discourse-cdn.com/v4/letter/a/3ec8ea/32.png) [@aguy-PG](https://forum.juce.com/u/aguy-PG)\
**Post date:** [March 22, 2023, 8:04pm UTC](https://forum.juce.com/t/truncate-combobox-text-without-changing-the-selectedid/16778/6 "2023-03-22T20:04:20Z")

</div>

Bump!

This is a great feature request! I have a combo box with items and I would like the item in the list to appear differently than whats shown in the text editor when selected. An option to set the string to be used in the popup menu if different then the one in the label.

---

<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:** [March 22, 2023, 8:52pm UTC](https://forum.juce.com/t/truncate-combobox-text-without-changing-the-selectedid/16778/7 "2023-03-22T20:52:47Z")

</div>

Totally agree! I have a nasty hack in there currently to make this work.
