java/swing: clipboard paste

若如初见. 提交于 2019-11-29 16:24:19
Kingsolmn

This answer by Bozhidar Batsov is the best solution I have seen around, simple and extensible. 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,
  2. Add the action to the enum Actions
  3. Add an AbstractAction with the desired functions
  4. Add the new action to the popup
  5. Add the needed logic to the mouseClicked(MouseEvent e) method

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!

As shown in the intro and the ListCutPaste demo, the two are connected. The DnD gets you CCP automatically.

I would instead suggest you to take a look at java.awt.datatransfer.Clipboard class documentation. I think it will go along with your DnD operations.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!