Set ComboBox font, what am I doing wrong?


#1

Hi,

I’m trying to set a custom font for a ComboBox by overriding LookAndFeel_V3::getComboBoxFont() in a custom look and feel class and then setting the ComboBox to use an instance of that class for it’s look and feel. However, it just uses the default font regardless of the Font returned by getComboBoxFont(). JUCE v 4.3.1, OS X 10.12.1.

Minimal code:

class BoxLookAndFeel : public LookAndFeel_V3
{
    Font getComboBoxFont (ComboBox& box) override
    {
        return Font ("Comic Sans MS", "Regular", 20.f);
    }
};

class MainContentComponent   : public Component
{
public:
    MainContentComponent()
    {
        box.setLookAndFeel(&landf);
        box.addItemList(StringArray({"Foo", "Bar", "Baz", NULL}), 1);
        
        addAndMakeVisible(box);            
        setSize (600, 400);
    }
    
    void resized() override
    {
        box.setBounds(50, 50, 100, 50);
    }

private:
    BoxLookAndFeel landf;
    ComboBox box;
    
};

#2

That font is for the label that shows when the ComboBox is not showing its popup menu.
You need to override getPopupMenuFont() to change the font in the menu that pops up.

class BoxLookAndFeel : public LookAndFeel_V3
{
public:
    Font getComboBoxFont (ComboBox& /*box*/) override
    {
        return getCommonMenuFont();
    }
    
    Font getPopupMenuFont() override
    {
        return getCommonMenuFont();
    }
    
private:
    Font getCommonMenuFont()
    {
        return Font ("Wingdings", "Regular", 20.f);
    }
};

class MainContentComponent   : public Component
{
public:
    MainContentComponent()
    {
        box.setLookAndFeel (&landf);
        box.addItemList(StringArray ({"Foo", "Bar", "Baz"}), 1);
        
        addAndMakeVisible(box);
        setSize (600, 400);
    }
    
    void resized() override
    {
        box.setBounds(50, 50, 100, 50);
    }
    
private:
    BoxLookAndFeel landf;
    ComboBox box;
};

#3

Aha! Thanks… missed that one.