e642. 处理拖放事件

The drop target in this example only accepts dropped String objects. A drop target must implement DropTargetListener and supply an implementation for drop().

    public void drop(DropTargetDropEvent evt) {
        try {
            Transferable t = evt.getTransferable();
    
            if (t.isDataFlavorSupported(DataFlavor.stringFlavor)) {
                evt.acceptDrop(DnDConstants.ACTION_COPY_OR_MOVE);
                String s = (String)t.getTransferData(DataFlavor.stringFlavor);
                evt.getDropTargetContext().dropComplete(true);
                process(s);
            } else {
                evt.rejectDrop();
            }
        } catch (IOException e) {
            evt.rejectDrop();
        } catch (UnsupportedFlavorException e) {
            evt.rejectDrop();
        }
    }

 

Related Examples
posted @ 2018-09-02 21:12  borter  阅读(107)  评论(0编辑  收藏  举报