简体   繁体   English

java / swing:剪贴板粘贴

[英]java/swing: clipboard paste

I have a DropTargetListener setup to allow me to drag + drop strings into some tables of my Swing application -- on a drop, I parse the string and insert data into the table. 我有一个DropTargetListener设置,允许我将字符串拖放到Swing应用程序的某些表中-在拖放时,我解析字符串并将数据插入表中。

I would like to do the same thing with a clipboard paste (Ctrl-V). 我想用剪贴板粘贴(Ctrl-V)做同样的事情。 What literature is there to explain how to handle clipboard pastes? 有哪些文献可以解释如何处理剪贴板粘贴? I'm looking at some stuff from Sun's website and it seems bizarre, like this should be simpler. 我正在看Sun网站上的东西 ,这看起来很奇怪,像这样应该更简单。

This answer by Bozhidar Batsov is the best solution I have seen around, simple and extensible. Bozhidar Batsov的答案是我所见过的最好的解决方案,既简单又可扩展。 After adding his class file, this is how I implemented his class 添加他的课程文件后,这就是我实现他的课程的方式

txtTextField.addMouseListener(new ContextMenuMouseListener());

Adding additional actions should be pretty simple: 添加其他操作应该非常简单:

  1. add a new Action class field, 添加一个新的Action类字段,
  2. Add the action to the enum Actions 将动作添加到enum Actions
  3. Add an AbstractAction with the desired functions 添加具有所需功能的AbstractAction
  4. Add the new action to the popup 将新动作添加到弹出窗口
  5. Add the needed logic to the mouseClicked(MouseEvent e) method 将所需的逻辑添加到mouseClicked(MouseEvent e)方法

And there you go. 然后你去。 I added the detail here partly to make sure I understand it, and also to give those after me a clear understanding of what needs to be done. 我在此处添加详细信息的部分目的是确保我理解它,也使我之后的人们对需要做的事情有清楚的了解。 Don't forget to include the necessary imports ! 不要忘记包括必要的imports

As shown in the intro and the ListCutPaste demo , the two are connected. 简介ListCutPaste 演示中所示 ,两者已连接。 The DnD gets you CCP automatically. DnD会自动为您提供CCP。

I would instead suggest you to take a look at java.awt.datatransfer.Clipboard class documentation. 相反,我建议您看一下java.awt.datatransfer.Clipboard类文档。 I think it will go along with your DnD operations. 我认为它将与您的DnD操作一起进行。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM