ComboBox TextEditor

Hi all :slight_smile:
I’m trying to customise the TextEditor of a ComboBox.
So I create a class CustomComboBox which inherits from ComboBox.

class CustomComboBox    : public ComboBox,
                             Label::Listener
{
public:
    CustomComboBox()
    {
    }

    ~CustomComboBox()
    {
    }
    void labelTextChanged (Label* labelThatHasChanged) override {
        DBG("labelTextChanged");
    }

    void editorShown (Label* l, TextEditor& e) override
    {
        DBG("EditorShown");
    }

This is described in the ComboBox Tutorial:
https://docs.juce.com/master/tutorial_combo_box.html

However I neither reach editorShown nor labelTextChanged.

What am i missing?

Thanks guys!

I don’t see where that article describes adding those functions to your CustomComboBox. Those are members of a Label, not of a ComboBox. The ComboBox contains a label (when editing). I’m not sure if this is the only way to do this, but you can override the ComboBox’s LookAndFeelMethods function createComboBoxTextBox(), from which you can return a custom Label-derived class in which you have overridden those functions. It would be that Label-derived class that you would customize for this behavior, not the ComboBox itself.

1 Like

Ah thanks. I’ll try that!

The article reads:

A more sophisticated method to filter input text would be to implement a custom class that derives from the ComboBox class. Then you can override its Label::Listener::editorShown() virtual function to customise its text editor. You can use the TextEditor::setInputFilter() function to use a TextEditor::InputFilter object to filter the text entered before it even appears on the screen.

Interesting, since ComboBox is not derived from Label, and has no access to Label::Listener. I wonder if that is old commentary from a time when ComboBox was derived from a Label?

I don’t think it ever was. It also says “to implement a custom class that derives from the ComboBox class”. I think the only place to get hold of the private label is really the creation callback in the lookAndFeel, which is a bit quirky…

If using the ComboBoxes label for input validation is a use case, it should either forward the events from the label to the ComboBox::Listener (making ComboBox a Label::Listener and adding the necessary events), or give access to the underlying label.

1 Like