I’m hoping there’s a simple answer to this, as it seems a simple problem! It’s got me stumped though!
I’ve got a parent Component. I have 3 non-overlapping ImageComponents which are children of the parent.
When I mouseover the child components I would like them to respond. However, no matter what I try, I cannot get any mouse events from the child components! Yes, I can use a MouseListener so that they receive events from the parent, but the only events fired from the parent are mouseEnter and mouseExit when the mouse goes outside the bounds of the parent.
There are no events when the mouse is inside the parent and moving over the child components.
I saw a post where it was suggested that the parent should fire a mouseExit when the mouse moves over a child component - is that correct? If so, I’m not seeing that.
Any ideas?
Are you listening in the parent for mouse events from the children?
If you want to do that, you first have to add a mouse listener for the children. To do so, add the following line in the parent (e.g. in the constructor):
“childComp.addMouseListener(this, true);”
That is really not necessary. Either the ImageComponent can react by its own, by inheriting and overriding mouseDown, or you call on your parent: setInterceptMouseClicks (true, false);
This will make the component handle all mouse events by itself, also the ones, that happen on their child components.
Only if you need to forward events from a specific child component, but not for others, then you can use a mouseListener. But a clever hierarchy usually avoids that.
Yep, I have created an ImageComponent subclass which overrides mouseEnter and mouseExit. They are the only mouse events I need from the child components. Definitely marked them as override.
Right, I think I’ve found the issue. I was calling setInterceptMouseClicks(false, false) in the constructor of my ImageComponent subclass. I had assumed that only had an effect on mouse clicks, not all mouse events?!
To be fair, it mentions to “Change[s] the default return value for the hitTest() method.” - but that would only tell you something, once you had more exposure with mouse handling in general.
In this method you only see the flags for hitTest, so an average user wouldn’t make that connection immediately.
Since it is specifically called “mouseClicks”, an addition, that this affects ALL mouse events wouldn’t hurt IMHO.
The point is I didn’t suspect that setInterceptMouseClicks was the source of my problem, since the problem was not mouse click-related. I have been using Juce for a long time and make a point of understanding what’s going on under the hood.
It would be nice if it was possible to still get mouseEnter/Exit/Move events without intercepting mouse clicks. I need this quite a bit and it seems to require a different annoying fix each time