How to grab mouse click in top-level component?

Seems like there should be a straightforward way to do this, so I’m hoping I’m just missing someting obvious… So, I have a top-level component that has a bunch of subcomponents in it (sliders etc). Normally the subcomponents should handle mouse clicks and do their thing, and that all works automatically. But, if the user right-clicks anywhere on the top-level component (including clicking on top of any of the subcomponents), I want to display a popup menu. It’s going to be the same menu no matter where they click, so I want to handle it in the top-level component code. I know how to display the menu etc, but how do I intercept the mouse click in the top-level component before any subcomponents get a shot at it, and only do something with it if certain modifiers are set, but otherwise have it dispatch the mouse click to the appropriate subcomponent as usual?

You can use Component::addMouseListener to intercept events for all child components.

Thanks. I had looked at that but couldn’t see how to prevent the event from propagating once the MouseListener handled it. I would want to install a MouseListener on the component (specifying true for wantsEventsForAllNestedChildComponents), and in that listener I’d check for a right-click and display my popup menu. But, if I actually handle the click and display the popup, I don’t want the event to still be handled by the underlying component after I’m done with it. How do I do that?

Hmm, there’s currently no way to stop an event propagating…

Ah. Well consider it a feature request. Would be very useful for doing something like what I’m trying to do, which honestly seems like it should be fairly straightforward. I believe PowerPlant had a kinda similar functionality via “Attachments”. That was a little more general mechanism but the idea applies. You could specify whether or not the host you attached to should handle the event after your attachment code got a shot at it.

But in lieu of that… what’s the best way to do what I want to do? Change all the subcomponents to check for a right-click and not do anything in that case?

Yeah, it’d actually be quite difficult to add a feature like that, because events don’t really ‘propagate’ via the parent components, they get sent directly to the target component, having selected it with hitTest. I’ll have a think about it.