MouseMove / MouseDrag on contiguous Components


I have problem with the MouseMove/MouseDrag management. Here is a little schematic:

All the Components are built dynamically. The user can select several CellComponents by clicking on one and moving the mouse till it reaches any other CellComponents. Basically, the selected CellComponents are those that are contained in the rectangle starting where the first MouseDown happened, ending where the mouse is now (it works exactly as an Excel selection).

To do that, I have my CellComponents override the MouseDown method, notifying the PatternComponent, which in turns detects that a selection has started.

At that moment, the CellComponent::mouseDrag is triggered whenever the mouse moves. The problem is: only the first clicked CellComponent will send the mouseDrag event because the drag started on it. If the mouse goes over any other CellComponent, I can NOT detect it. I tried to use the mouseMove event, but it isn't sent anymore as soon as a mouse button is down.

A solution could be to change mouseMove code (change the Component original behavior) to always send the mouseMove, regardless of the button state, but I don't know if it's the best way.

Maybe use some lower-level event I don't know about? Basically, I really want to avoid calculating which Component has been clicked from raw mouse events data.

Thanks for you help.

Yeah, that's how GUI toolkits typically work, and it's done like that for good reasons (although that may be hard to see when you're trying to make something work differently).

There are lots of ways around it, e.g. add a MouseListener to the top-level component that would see all events in all the children. Or restructure your child components to ignore mouse events and let the parent deal with them. Or make each one watch for mouse-drags moving over another one, and foward them to it.. Lots of options, but admittedly this is a messy use-case to implement.

Considered using the LassoComponent?

Thanks for your ideas.


About the LassoComponent, I didn't know about it, and will probably use it later, but it doesn't fit this current use-case, because my custom selection also implies labels within my CellComponent (I highlight them inside according to the mouse position).


Anyway, I managed to do something that seems to work so far, by having the moveDrag event of the CellComponent ask the top-parent (PatternComponent) to find the child that matches the mouse coordinates (by using the mouseEvent.getEventRelativeTo method). From here I can manage the selection by myself. Not very beautiful, but the code seems solid enough.



1 Like