Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/326.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java DragSource-启动DragAction时出现InvalidDnDOperationException_Java_Swing_Drag And Drop - Fatal编程技术网

Java DragSource-启动DragAction时出现InvalidDnDOperationException

Java DragSource-启动DragAction时出现InvalidDnDOperationException,java,swing,drag-and-drop,Java,Swing,Drag And Drop,我的应用程序包含一些应该由DnD移动的JPanel。我已经实现了所有的侦听器和处理程序,一切似乎都是正确的。当用鼠标拖动一个面板时,我的DragGestureListener会意识到这一点,并希望启动新的DragAction: @Override public void dragGestureRecognized(DragGestureEvent dge) { // When the drag begins, we need to grab a referenc

我的应用程序包含一些应该由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”类型才能正确启动拖动。关键是我的JPanel不是一个窗口,上面提到的异常会被抛出

我做错了什么


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

必须为“窗口”类型,回答问题“我做错了什么?”?也就是说,臭烘烘的部分a)为什么要将面板作为数据放在表格中?b) 不管怎样,为什么不使用标准的swing/table拖动(也称为自定义TransferHandler),如果没有SSCCE演示我解决的问题,很可能没有任何帮助。我忘记设置table.setDragEnabled(true)和table.setDropMode(DropMode.INSERT);Thx kleopatra为您提供帮助:-)很高兴您解决了它:-)@Randbedingung不要让已解决的问题悬而未决!如果你找到了一个解决方案,把它作为答案贴出来,然后接受它:3