How to allow user to copy, cut and paste selected words from JTextArea?

Let's say I created a GUI with its main panel JTextArea and the superscript above (which contains a copy and paste button, abbreviated ctrl + c and ctrl + v).

Perhaps after some work the program will spit out a bunch of data on JTextArea, and then I want to allow the user to copy, cut or paste the selected (selected) words from JTextArea to other places, such as notepad or the like.

So, which listener should use to determine the choice, output the selected words and place them in the place where the OS stores the copied files? Plz be specific, thanks for any help! :)

+4
source share
2 answers

OS shortcuts for cutting, copying and pasting work by default in JTextArea .

However, JTextArea extends JTextComponent that has cut() , copy() and paste() methods that you can call if you want to add custom actions (context menu or top menu items or toolbar buttons) for these operations.

+9
source

To add Dan to the answer, JTextArea provides cut() , copy() and paste() methods that will handle the actual actions of a particular OS for yours.

Roseindia.net has a code for reference .

There are other copy methods that run in similar threads like this one :)

+6
source

Source: https://habr.com/ru/post/1433210/


All Articles