views:

69

answers:

2

Hello community,

I am trying to create a (child) JFrame which slides out from underneath one side of a second (parent) JFrame. The goal is to then have the child follow the parent around when it is moved, and respond to resizing events. This is somewhat related to this question.

I have tried using a ComponentListener, but with this method the child only moves once the parent has come to a stop, whereas I would like the child to move as the parent is dragged around the screen.

Another option I attempted was to start a new refresher thread that continually updated the child's location using getLocation() or getLocationOnScreen(), but the lag was the same as with ComponentListener.

Is there a way to get the true actual location of a JFrame even in the midst of a drag? or if not, is there a way to get the effect of a sheet sliding out from underneath and following the Frame around?

A: 

If it doesn't work on Mac, you can also store the initial position when dragging starts, then the location of the window is initialPosition + mouse movement.

Anthony
Could you be a little more specific about how to get the initial position and figure out when the drag stops?
Ian Fellows
A: 

You should use the mouseDragged(MouseEvent e) method of the MouseAdapter class. As you drag your main window around, you will get many of these mouseDragged events - handle each one by moving the second window around to a position that's relative to the location of the main window.

If you see 'lag' or if it doesn't reposition until the main window stops, try forcing Swing to repaint by calling repaint() on the second window after repositioning and/or by putting your repositioning code inside a SwingUtilities.invokeLater(...) block:

SwingUtilities.invokeLater(new Runnable()
{
    @Override
    public void run()
    {
        // your logic here
    }
});

Let me know if this helps.

Warlax