# 把它们放在一起--DnD 和 CCP

> 原文： [https://docs.oracle.com/javase/tutorial/uiswing/dnd/together.html](https://docs.oracle.com/javase/tutorial/uiswing/dnd/together.html)

我们已经展示了如何实现拖放支持以及如何实现剪切，复制和粘贴支持。你如何将两者结合在一个组件中？

你在`TransferHandler`的`importData`方法中实现这两个方法，如下所示：

```
if (transferSupport.isDrop()) {
    // put data in transferSupport.getDropLocation()
} else {
    // determine where you want the paste to go (ex: after current selection)
    // put data there
}

```

在非文本组件页面中 [CCP 上讨论的`ListCutPaste`示例同时支持 dnd 和 ccp。这是它的`importData`方法（`if` - `else`丢弃逻辑用粗体表示）：](listpaste.html)

```
    public boolean importData(TransferHandler.TransferSupport info) {
        String data = null;

        //If we cannot handle the import, bail now.
        if (!canImport(info)) {
            return false;
        }

        JList list = (JList)info.getComponent();
        DefaultListModel model = (DefaultListModel)list.getModel();
        //Fetch the data -- bail if this fails
        try {
            data = (String)info.getTransferable().getTransferData(DataFlavor.stringFlavor);
        } catch (UnsupportedFlavorException ufe) {
            System.out.println("importData: unsupported data flavor");
            return false;
        } catch (IOException ioe) {
            System.out.println("importData: I/O exception");
            return false;
        }

        if (info.isDrop()) { //This is a drop
            JList.DropLocation dl = (JList.DropLocation)info.getDropLocation();
            int index = dl.getIndex();
            if (dl.isInsert()) {
                model.add(index, data);
                return true;
            } else {
                model.set(index, data);
                return true;
            }
        } else { //This is a paste
            int index = list.getSelectedIndex();
            // if there is a valid selection,
            // insert data after the selection
            if (index >= 0) {
                model.add(list.getSelectedIndex()+1, data);
            // else append to the end of the list
            } else {
                model.addElement(data);
            }
            return true;
        }
    }

```

这是您需要安装`if` - `else`逻辑以区分 dnd 和 ccp 的唯一地方。