问题
This is my first question, so bear with me. Let's say that I have 2 JPanel and 1 JToolBar. What I want to do is drag the toolbar from one panel to another and, after mouse release, the toolbar should stick to the second.
回答1:
BasicToolbarUI has floatAt method. As you can see from source (below) toolbar uses docing source which is toolbar's parent container. You can try to override the method and replace the source.
protected void floatAt(Point position, Point origin)
{
if(toolBar.isFloatable() == true)
{
try
{
Point offset = dragWindow.getOffset();
if (offset == null) {
offset = position;
dragWindow.setOffset(offset);
}
Point global = new Point(origin.x+ position.x,
origin.y+position.y);
setFloatingLocation(global.x-offset.x,
global.y-offset.y);
if (dockingSource != null) {
Point dockingPosition = dockingSource.getLocationOnScreen();
Point comparisonPoint = new Point(global.x-dockingPosition.x,
global.y-dockingPosition.y);
if (canDock(dockingSource, comparisonPoint)) {
setFloating(false, comparisonPoint);
} else {
setFloating(true, null);
}
} else {
setFloating(true, null);
}
来源:https://stackoverflow.com/questions/8266332/transfer-jtoolbar-from-one-jpanel-to-another