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?
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:
Let me know if this helps.