[Solved] ComboBox and MouseOver?


#1

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?


#2

setInterceptMouseClicks (false, false) on the child label


#3

Unfortunately the label is a priavte member of ComboBox.


#4

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

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