1. 程式人生 > >JTree的拖拽響應實現

JTree的拖拽響應實現

dragSource.addDragSourceMotionListener(new DragSourceMotionListener() {
                public void dragMouseMoved(DragSourceDragEvent dge) {
                    // TODO 自動生成方法存根
                    DragSourceContext dsc = dge.getDragSourceContext();
                  
                     //拖拽過程中依據滑鼠位置改變圖示
                    Point pt = getMousePosition();
                    if (pt == null)
                        return;
                    TreePath   tp   =   getPathForLocation(pt.x, pt.y);  
                    java.awt.Cursor cursor = null;
                    if (tp != null) {
                        setSelectionPath(tp);
                        cursor = DragSource.DefaultCopyDrop;
                    }else {
                        setSelectionPath(null);             
                        cursor = DragSource.DefaultCopyNoDrop;
                    }
                  
                    dsc.setCursor(cursor);
                }
              });
          }