How to move a dockable into a different CContentArea

This is different from Move dockable to other CContentArea?.

I have two JFrames, each with a CContentArea. I create two DefaultSingleCDockables. Both dockables appear in the same content area:

I can use the mouse to drag and drop Dockable 2 into JFrame 2:

How can I move Dockable 2 into JFrame 2 using code?

Complete example code:

import bibliothek.gui.dock.common.CContentArea;
import bibliothek.gui.dock.common.CControl;
import bibliothek.gui.dock.common.DefaultSingleCDockable;
import java.awt.BorderLayout;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.SwingUtilities;
import javax.swing.WindowConstants;
public class MoveDockable {
    private final static int JFRAME_WIDTH = 300;
    private final static int JFRAME_HEIGHT = 400;
    public static void main(String[] args) {
        SwingUtilities.invokeLater(() -> {

            JFrame jFrame1 = new JFrame("JFrame 1");
            jFrame1.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
            jFrame1.getContentPane().setLayout(new BorderLayout());
            jFrame1.setSize(JFRAME_WIDTH, JFRAME_HEIGHT);
            jFrame1.setLocation(0, 0);
            CControl cControl = new CControl(jFrame1);
            CContentArea contentArea1 = cControl.createContentArea("contentAreaID1");
            jFrame1.getContentPane().add(contentArea1);
            DefaultSingleCDockable dockable1 = new DefaultSingleCDockable(
                    "dockableID1", "Dockable 1", new JLabel("Contents of dockable 1"));
            cControl.addDockable(dockable1);
            dockable1.setVisible(true);
            jFrame1.setVisible(true);

            JFrame jFrame2 = new JFrame("JFrame 2");
            jFrame2.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
            jFrame2.getContentPane().setLayout(new BorderLayout());
            jFrame2.setSize(JFRAME_WIDTH, JFRAME_HEIGHT);
            jFrame2.setLocation(JFRAME_WIDTH, 0);
            CContentArea contentArea2 = cControl.createContentArea("contentAreaID2");
            jFrame2.getContentPane().add(contentArea2);
            DefaultSingleCDockable dockable2 = new DefaultSingleCDockable(
                    "dockableID2", "Dockable 2", new JLabel("Contents of dockable 2"));
            cControl.addDockable(dockable2);
            dockable2.setVisible(true);
            jFrame2.setVisible(true);
        });
    }
}

None of these lines do what I want:

dockable2.setLocation(contentArea2.getCenterArea().getAutoBaseLocation(true));
dockable2.setLocation(contentArea2.getCenterArea().getAutoBaseLocation(false));
dockable2.setLocation(contentArea2.getCenterArea().getBaseLocation());
dockable2.setLocation(contentArea2.getCenterArea().getDropLocation());
dockable2.setLocation(contentArea2.getCenterArea().getStationLocation());

There is a piece of code (CStationContainerHistoryRewriter) that tries to ensure, that a Dockable stays in the same root-container as long as possible. This is important when using buttons like „maximze“, otherwise the Dockables would be jumping around.

Unfortunatelly that feature catches your call to setLocation.

Solution 1:
Call setLocation before calling dockable2.setVisible(true). This way dockable2 has not yet a root-container, and thus the anti-jump-feature will not be triggered.

Solution 2
Make dockable2 invisible before putting it at its new place:

dockable2.setVisible(false);
dockable2.setLocation(contentArea2.getCenterArea().getDropLocation());
dockable2.setVisible(true);

Solution 3
If you only need the feature to set up an initial layout: you can use a CGrid to set the initial position of dockable2 (and other dockables on the same JFrame).
deploy even makes the Dockables visible, and registers them at the CControl:

		DefaultSingleCDockable dockable2 = new DefaultSingleCDockable(
				"dockableID2", "Dockable 2", new JLabel("Contents of dockable 2"));
		DefaultSingleCDockable dockable3 = new DefaultSingleCDockable(
				"dockableID3", "Dockable 3", new JLabel("Contents of dockable 3"));

		CGrid grid = new CGrid(cControl);
		grid.add(0, 0, 50, 100, dockable2); // left 50%
		grid.add(50, 0, 50, 100, dockable3); // right 50%
		contentArea2.deploy(grid);

		jFrame2.setVisible(true);
2 „Gefällt mir“