ComboBox 'textWhenNothingSelected' Font

in ComboBox::paint(), the font used to draw the textWhenNothingSelected is set this way :

g.setFont (label->getFont());

but this will only work fine if the label’s font has not been specified/overridden by the current lookAndFeel. Otherwise that “textWhenNothingSelected” will get a different font than the combobox selected item text.

Could that be changed to the following?

g.setFont (getLookAndFeel().getLabelFont (*label));

Not sure when this would be an issue though as the label is a child class of the ComboBox so should inherit any look and feel changes from the parent, right?

If you’ve overriden LookAndFeel::createComboBoxTextBox to return a label with a custom look and feel then I would expect the “textWhenNothingSelected” to follow that look and feel, right?

The label has the right lookAndFeel.
The problem is that the font used to draw the "textWhenNothingSelected " is not set using the lookandfeel method (i.e getLabelFont()).

here is a short code to reproduce (with screenshot below) :

struct MyLookAndFeel : public LookAndFeel_V4
{
    Font getLabelFont (Label&) override
    {
        return { 30.f };
    }
};

//==============================================================================
class MainContentComponent   : public Component
{
public:
    //==============================================================================
    MainContentComponent()
    {
        comboBox.setTextWhenNothingSelected ("nothing");
        comboBox.addItem ("first item", 1);
        comboBox.setLookAndFeel (&lnf);

        comboBox2.setTextWhenNothingSelected ("nothing");
        comboBox2.addItem ("first item", 1);
        comboBox2.setLookAndFeel (&lnf);

        addAndMakeVisible (comboBox);
        addAndMakeVisible (comboBox2);
        setSize (600, 400);
    }

    void paint (Graphics& g) override
    {
        g.fillAll (Colours::lightgrey);
    }

    void resized() override
    {
        comboBox.setBounds (10, 10, 200, 20);
        comboBox2.setBounds (10, 50, 200, 20);
    }

private:
    //==============================================================================
    MyLookAndFeel lnf;
    ComboBox comboBox;
    ComboBox comboBox2;
};

58

note that the “textWhenNothingSelected” is just a string, drawn independently from the label. Perhaps it would just make more sense to just set the label text this “textWhenNothingSelected” whenever appropriate

Ok I’ve fixed the issue by doing this:

g.setFont (label->getLookAndFeel().getLabelFont (*label));

This would also take the label’s look and feel into account as this could be different from the combobox (as the label is created by a look and feel method).

This is now on develop with commit 8e9ede.

1 Like