[Solved] ComboBox and MouseOver?

I want to give my ComboBox a custom LookAndFeel: Whenever the mouse is over the ComboBox, I want to highlight it.

The problem: A ComboBox has a Label inside, which holds the text of the ComboBox. As soon as the mouse touches the label, it is no longer over the ComboBox (even thoug it is still inside the ComboBox). And therefore the highlighting does not work. 

What is the best way to solve this problem?

setInterceptMouseClicks (false, false) on the child label

1 Like

Unfortunately the label is a priavte member of ComboBox.

I found a solution. What I have to do is.

- subclass ComboBox

- in the constructor add a mouse listener to @self and tell it to also intercept mouse events of the children

- override mouseEnter and mouseExit and make them call repaint()

- in the LookAndFeel::drawComboBox function use isMouseOver(true). The "true" means that it also includes child components.

Here is a codesnippet:

class ExtendedComboBox : public ComboBox
{
public:
    ExtendedComboBox()
        :ComboBox()
    {
        addMouseListener(this, true);
    }

    void mouseEnter(const MouseEvent &event) { repaint(); }

    void mouseExit(const MouseEvent &event)  { repaint(); }
};

And in the LookAndFeel::drawComboBox function:

void MyAppLookAndFeel::drawComboBox(Graphics& g, int width, int height, bool isButtonDown,
    int buttonX, int buttonY, int buttonW, int buttonH, ComboBox& box)
{
    if (box.isMouseOver(true))
    {
        ...
    }
    else
    {
        ...
    }
}
5 Likes

Just had the same problem and then found this. Since the ComboBox constructor is explicitly calling

setRepaintsOnMouseActivity (true);

in its constructor, shouldn’t we expect this to “just work”?

If there is no intent to fix this, can the call be removed and maybe a comment be left, so future developers won’t waste their time with something that can’t work?

Thanks,
Mike

I came across this because I wanted to have a pointing hand cursor when interacting with ComboBox: setMouseCursor (juce::MouseCursor::StandardCursorType::PointingHandCursor);

But the same issue occurs where the ComboBox's child Label intercepts the mouse clicks / mouse pointer interaction. My solution was to make a custom LookAndFeel and override ComboBox::LookAndFeelMethods::createComboBoxTextBox, and when the Label is created, I set it to not intercept mouse clicks:

Label * createComboBoxTextBox (ComboBox & combo) override
{
    Label * label = new Label (String(), String());
    label->setInterceptsMouseClicks (false, false);
    return label;
}
5 Likes