JTextArea.copy () clipboard is cleared when the program exits

I have a JDialog with a JTextArea component. This dialog box shows the user what arguments to use when starting the program to start the mode they just configured. I have a button in the dialog box to copy the arguments to the clipboard. This uses the copy () method for the JTextArea object.

This works fine, and the clipboard contains the correct text until the program is closed. Then the clipboard is lost. Is there anyway to save this after the program ends? The usual operation would then be to exit the program and start it again with arguments.

This may seem odd, but the idea is that the user will set up a GUI environment and then run it with arguments in cron or similar.

+3
source share
5 answers

I just found out that there are two clipboards in Java, local and system. Would explain something if you just took the local clipboard.

Here is an example that uses the system clipboard. Hope it solves your problem!

+1
source

Works well for me when I use Ctrl + C to copy the contents of a text component. So try using the attached action (which uses Ctrl + C) to copy instead of the copy () method:

JButton button = new JButton(DefaultEditorKit.CopyAction());
+1

Robot Ctrl + c

 Robot robot = new Robot();
 robot.keyPress(KeyEvent.VK_CONTROL);
 robot.keyPress(KeyEvent.VK_C);
 robot.keyRelease( KeyEvent.VK_C );
 robot.keyRelease( KeyEvent.VK_CONTROL );

, , .

StringSelection ss = new StringSelection("your text for clipboard");
Toolkit.getDefaultToolkit().getSystemClipboard().setContents(ss, null);

which works for me on windows 7.

+1
source

I would suggest using an API for this.

0
source

This is enough for me:

JButton buttonCopy = new JButton(new DefaultEditorKit.CopyAction());
buttonCopy.setText("copy");
0
source

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


All Articles