# Mouse wheel vs. child components

**URL:** <https://forum.juce.com/t/mouse-wheel-vs-child-components/50011>\
**Category:** General JUCE discussion\
**Created:** [February 4, 2022, 10:12am UTC](https://forum.juce.com/t/mouse-wheel-vs-child-components/50011 "2022-02-04T10:12:08Z")\
**Posts on this page:** 10\
**Page:** 1

<div class="post-metadata">

**Author:** ![Kraku](https://avatars.discourse-cdn.com/v4/letter/k/74df32/32.png) [@Kraku](https://forum.juce.com/u/Kraku)\
**Post date:** [February 4, 2022, 10:12am UTC](https://forum.juce.com/t/mouse-wheel-vs-child-components/50011/1 "2022-02-04T10:12:08Z")

</div>

I want to achieve the following functionality:

- Parent component reads and handles all mouse wheel events.
- Child components take care of all mouse clicks.

How to achieve that? It seems that the child components always handle mouse wheel and mouse clicks whenever the cursor is on top of them. Only when the cursor is not on top of any of the child components, then the parent seems to be handling the wheel.

---

<div class="post-metadata">

**Author:** ![reuk](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.juce.com/reuk/32/21494_2.png) [@reuk](https://forum.juce.com/u/reuk)\
**Post date:** [February 4, 2022, 11:01am UTC](https://forum.juce.com/t/mouse-wheel-vs-child-components/50011/2 "2022-02-04T11:01:18Z")

</div>

Two options would be:

- Add the parent component as a MouseListener of each of the child components, and handle mouse wheel events in the parent component
- Set the child components to ignore all mouse interaction (`setInterceptsMouseClicks (false, false)`), and handle all mouse events in the parent component

---

<div class="post-metadata">

**Author:** ![Mrugalla](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.juce.com/mrugalla/32/13092_2.png) [@Mrugalla](https://forum.juce.com/u/Mrugalla)\
**Post date:** [February 4, 2022, 11:21am UTC](https://forum.juce.com/t/mouse-wheel-vs-child-components/50011/3 "2022-02-04T11:21:19Z")

</div>

beware that if you use setInterceptsMouseClicks(false, false) as a solution there might be some additional things that don’t work for those children anymore then. can be quite some edge case stuff like when you have a custom cursor image for specific components it only gets updated if the component accepts mouse events. if your child components are designed in a way that you define the mouse methods (mouseUp, mouseDown etc.) yourself you can also put callbacks in there like std::function\<void()\> that trigger an action which involves stuff in the parent component. that way you can ensure that all other component features still work the same, but if your child component is not juce::Component itself but some derived class of Component you should call the base class’ function of your mouseMethods in them as well to make sure to not interfere with existing functionality. that can look like this:

```auto
void mouseEnter(const juce::MouseEvent& evt) override
{
    onEnterCallback();
    WhateverTheBaseClassIs::mouseEnter(evt);
}

```

---

<div class="post-metadata">

**Author:** ![Kraku](https://avatars.discourse-cdn.com/v4/letter/k/74df32/32.png) [@Kraku](https://forum.juce.com/u/Kraku)\
**Post date:** [February 4, 2022, 12:39pm UTC](https://forum.juce.com/t/mouse-wheel-vs-child-components/50011/4 "2022-02-04T12:39:30Z")

</div>

I already tried making the parent a MouseListener. The result was that parent responded to mouse wheel only when there was no child component under the mouse cursor. So it didn’t work properly.

With your option 2 do you mean that I forward all the mouse events to child components from the parent component?

---

<div class="post-metadata">

**Author:** ![reuk](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.juce.com/reuk/32/21494_2.png) [@reuk](https://forum.juce.com/u/reuk)\
**Post date:** [February 4, 2022, 12:40pm UTC](https://forum.juce.com/t/mouse-wheel-vs-child-components/50011/5 "2022-02-04T12:40:40Z")

</div>

The first option is probably the better option. Did you remember to call `addMouseListener` on each of the child components, to register the parent as a listener of the children?

---

<div class="post-metadata">

**Author:** ![Kraku](https://avatars.discourse-cdn.com/v4/letter/k/74df32/32.png) [@Kraku](https://forum.juce.com/u/Kraku)\
**Post date:** [February 4, 2022, 12:45pm UTC](https://forum.juce.com/t/mouse-wheel-vs-child-components/50011/6 "2022-02-04T12:45:21Z")

</div>

The problem with that method is that the more I have different kinds of child components, the more I need to pass such callback functions as parameters into the child hierarchies. It would be fantastic if there was a way to put all that functionality into one place and be done with it.

---

<div class="post-metadata">

**Author:** ![Kraku](https://avatars.discourse-cdn.com/v4/letter/k/74df32/32.png) [@Kraku](https://forum.juce.com/u/Kraku)\
**Post date:** [February 4, 2022, 12:46pm UTC](https://forum.juce.com/t/mouse-wheel-vs-child-components/50011/7 "2022-02-04T12:46:40Z")

</div>

Umm, no I didn’t. If I use addMouseListener on all the child components, does that mean that every time child component handles a mouse event, that same event gets passed on to the parent as well?

---

<div class="post-metadata">

**Author:** ![reuk](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.juce.com/reuk/32/21494_2.png) [@reuk](https://forum.juce.com/u/reuk)\
**Post date:** [February 4, 2022, 12:48pm UTC](https://forum.juce.com/t/mouse-wheel-vs-child-components/50011/8 "2022-02-04T12:48:27Z")

</div>

Yes, but you can use `MouseEvent::originalComponent` to find the component that originally received the event, and check whether or not it is the parent component.

---

<div class="post-metadata">

**Author:** ![Kraku](https://avatars.discourse-cdn.com/v4/letter/k/74df32/32.png) [@Kraku](https://forum.juce.com/u/Kraku)\
**Post date:** [February 4, 2022, 12:48pm UTC](https://forum.juce.com/t/mouse-wheel-vs-child-components/50011/9 "2022-02-04T12:48:58Z")

</div>

Awesome! I’ll try that method and see how it works.

Thanks!

---

<div class="post-metadata">

**Author:** ![Kraku](https://avatars.discourse-cdn.com/v4/letter/k/74df32/32.png) [@Kraku](https://forum.juce.com/u/Kraku)\
**Post date:** [February 4, 2022, 1:22pm UTC](https://forum.juce.com/t/mouse-wheel-vs-child-components/50011/10 "2022-02-04T13:22:07Z")

</div>

AddMouseListener seems to work great. The big plus side is that you only need to tell the first child component to add it’s parent to be the listener. Then if you set the second parameter in the method as “true”, every single child component of that child automatically sends their mouse events to the parent you set as a listener. So only couple of lines of code is required and no need to go and set every single child in the hierarchy to add a new listener.
