DragSource - 启动DragAction时出现InvalidDnDOperationException

时间:2013-02-07 15:25:28

标签: java swing drag-and-drop

我的应用程序包含一些可由DnD移动的JPanel。我已经实现了所有的监听器和处理程序,一切似乎都是正确的。使用鼠标拖动一个面板时,我的DragGestureListener会识别出并想要启动一个新的DragAction:

    @Override
    public void dragGestureRecognized(DragGestureEvent dge) {

        // When the drag begins, we need to grab a reference to the
        // parent container so we can return it if the drop
        // is rejected
        Container parent = getPanel().getParent();

        setParent(parent);

        // Remove the panel from the parent.  If we don't do this, it
        // can cause serialization issues.  We could over come this
        // by allowing the drop target to remove the component, but that's
        // an argument for another day
        parent.remove(getPanel());

        // Update the display
        parent.invalidate();
        parent.repaint();

        // Create our transferable wrapper
        TicketTransferable transferable = new TicketTransferable(getPanel());

        // Start the "drag" process...

        dge.startDrag(null, transferable);

    }

然后发生以下异常:

exception in thread "AWT-EventQueue-0" java.awt.dnd.InvalidDnDOperationException: 
     Cannot find top-level for the drag source component
at sun.awt.X11.XDragSourceContextPeer.startDrag(XDragSourceContextPeer.java:126)
at sun.awt.dnd.SunDragSourceContextPeer.startDrag(SunDragSourceContextPeer.java:134)

我检查了XDragSourceContextPeer,发现触发DragAction的组件(在我的情况下,JPanel要自行移动)必须是“Window”类型才能正确启动拖动。 Point是我的JPanel不是一个窗口,所提到的Exception被抛回。

我做错了什么?

P.S。:所有“可移动”面板都位于JTable的不同单元格中。

0 个答案:

没有答案