0

I want to block dragging jInternalForm inside Desktop Pane. I've tried steps followed here : Preventing JInternalFrame from being moved out of a JDesktopPane

But it didn't work for me. Can someone suggest a working override method for this.

Community
  • 1
  • 1
  • Questions seeking debugging help ("why isn't this code working?") must include the desired behavior, a specific problem or error and the shortest code necessary to reproduce it in the question itself. Questions without a clear problem statement are not useful to other readers. See: How to create a [mcve]. – NightShadeQueen Aug 29 '15 at 15:48
  • Did you use the correct code? The "accepted" answer was only a proof of concept. The real solution was provided by the person who asked the question. – camickr Aug 29 '15 at 21:23

1 Answers1

0

Here is a somewhat simpler example of a custom DesktopManager to keep the internal frame within the bounds of the desktop:

public class BoundsDesktopManager extends DefaultDesktopManager
{
    /*
    **  This is called anytime a frame is moved. 
    **  This implementation keeps the frame from leaving the desktop.
    */
    @Override
    public void dragFrame(JComponent component, int x, int y)
    {
        // Deal only with internal frames

        if (component instanceof JInternalFrame)
        {
            JInternalFrame frame = (JInternalFrame)component;
            JDesktopPane desktop = frame.getDesktopPane();
            Dimension d = desktop.getSize();

            // Too far left or right?

            if (x < 0)
            {
                x = 0; 
            }
            else if (x + frame.getWidth() > d.width)
            {
                x = d.width - frame.getWidth();
            }

            //  Too high or low?

            if (y < 0)
            {
                y = 0;
            }
            else if (y + frame.getHeight() > d.height)
            {
                y = d.height - frame.getHeight();
            }
        }

        // Pass along the (possibly cropped) values to the normal drag handler.

        super.dragFrame(component, x, y);
    }
}
camickr
  • 321,443
  • 19
  • 166
  • 288