Bhaxy Bhaxy - 1 year ago 47
Java Question

What is the best way to cut, copy, and paste in Java?

I've created an application using Swing with a text area (JTextArea). I want to create an "edit" menu, with options to cut and copy text from the text area, and paste text from the clipboard into the text area.

I've seen a couple of ways to do this, but I wanted to know what the best way is. How should I implement the cut/copy/paste?

Answer Source

I would personally opt for re-using the standard cut, copy and paste actions. This is all explained in the Swing drag-and-drop tutorial: adding cut, copy and paste. The section about text components is the most relevant for you. A quick copy-paste of some code of that page:

menuItem = new JMenuItem(new DefaultEditorKit.CopyAction());