I need to restrict movement of a component (JInternalFrame) inside a JPanel.
More exact: a component should only move along one axis when dragged by the user.
I tried to do it by adding a component listener and reset the position
of one axis everytime the component moves. But it “vibrates”
(moves rapidly during dragging).
I even wrote a custom layout manager, it didn’t help a little!
I guess, the problem is: Both the layout manager and the listener
handle the movement-event after the component is actually moved, right?
Any suggestions?
Should (can I) intercept some event, and modify it, before it’s delivered?
you might want to subclass your InternalFrameUI. I see in
BasicInteralFrameUI.BorderListener.mouseDragged(event)(line 885 in version 1.129) a call togetDesktopManager().dragFrame(frame, newX, newY);You could extend
BasicInternalFrameUIto return a customDesktopManager(extension ofDefaultDesktopManager), where indragFrame()you test the identity of your frame and adjust the axis.edit:
thinking about it, you are right – it is too complex. as the UI for the internal frame defers to the
DesktopManagerof theJDesktopPane, you can set it there. here is a poc: