Truncate combobox text without changing the selectedID



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 I already know why this is happening:


    /** Sets the contents of the combo-box's text field.

        The text passed-in will be set as the current text regardless of whether

        it is one of the items in the list. If the current text isn't one of the

        items, then getSelectedId() will return -1, otherwise it wil return

        the approriate ID.

        @param newText          the text to select

        @param notification     determines the type of change notification that will

                                be sent to listeners if the text changes

        @see getText


    void setText (const String& newText,

                  NotificationType notification = sendNotificationAsync);

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




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. 


Thanks, that solves the problem.

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


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 addItem(), in which to specify the text to be used in the popup menu (if different from the one in the label)