Using ComponentBoundsConstrainer with dragging a component


I’m trying to use a ComponentBoundsConstrainer to stop components being dragged off the side of another component.

In my component that’s being dragged, I create a ComponentBoundsConstrainer and set the min on screen amounts:

bounds_ = new ComponentBoundsConstrainer();

The dragged component is a child of the component to constraint it’s position within.

To try and stop it being moved off the edge of the parent, in the dragged components moved() I call:


This works fine on the Mac, but on Windows it enters an endless loop calling moved() repeatedly when the component is dragged off the edge. I do something similar in resized() when resizing the component and this works fine on both Mac and Windows when resizing the component off the edge.

I’ve tried this with the latest from Git.

This seems like a bug because of the difference between Mac and Windows, but on the other, hand perhaps this isn’t the best way of doing this because there is a difference between the way Mac and Windows events are handled? If so, can anyone suggest a better way to do this avoiding this loop?

Many thanks

When moved() is called, it’s too late - the bounds have been set. You need to check the bounds before applying them to your component.

Ah ok, it didn’t seem like the best way to do this in moved()…

I fixed it by calling:

bounds_->setBoundsForComponent(this, newBounds, false, false, false, false);

in mouseDrag() once I’ve calculated the newBounds I want to apply from the dragging.