用于剪切/复制/粘贴的Java剪贴板

时间:2015-11-02 10:48:00

标签: java swt

我通过复制属于同一数据类型的多个项目来利用Java SWT剪贴板进行复制/粘贴。在这种情况下,当我从剪贴板中检索项目时,我总是将第一个匹配的项目放在剪贴板上。 有人可以就此分享他们的想法/意见吗?

例如:

我有三个项目说ABC具有相同的数据类型,然后当我尝试检索回来时,我总是得到项目A

  1. 这个问题怎么解决?
  2. 是否有其他剪贴板可用于复制/粘贴?
  3.   

    clipboard.setContents(new Object [] {data1},new Transfer []   {TextTransfer.getInstance()}); clipboard.setContents(new Object []   {data2},new Transfer [] {TextTransfer.getInstance()});

    试图检索时:

      

    字符串数据=   (字符串)clipboard.getContents(TextTransfer.getInstance());

    这里我们总是得到放在剪贴板上的data1,我们如何得到数据2?

2 个答案:

答案 0 :(得分:0)

创建一个包含项列表的容器对象。然后,您可以将该容器对象的实例放在剪贴板中,并根据需要从中加载任意数量的项目。

答案 1 :(得分:0)

Please see the javadoc of Clipboard.setContents. Calling setContents will clear the previous contents on the clipboard.

Javadoc: Place data of the specified type on the system clipboard. More than one type of data can be placed on the system clipboard at the same time. Setting the data clears any previous data from the system clipboard, regardless of type.

The javadoc also has an example showing how to set different transfer objects on the clipboard.

 Clipboard clipboard = new Clipboard(display);
 String textData = "Hello World";
 String rtfData = "{\\rtf1\\b\\i Hello World}";
 TextTransfer textTransfer = TextTransfer.getInstance();
 RTFTransfer rtfTransfer = RTFTransfer.getInstance();
 Transfer[] transfers = new Transfer[]{textTransfer, rtfTransfer};
 Object[] data = new Object[]{textData, rtfData};
 clipboard.setContents(data, transfers);
 clipboard.dispose();