Transfer jtoolbar from one jpanel to another

本秂侑毒 提交于 2019-12-11 02:01:44

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!