English 中文(简体)
JTextArea.copy() clipboard cleared when program exits
原标题:

I have a JDialog with a JTextArea component. This dialog shows the user what arguments to use when running the program to have it run the mode they just set up. I have a button on the dialog to copy the arguments to clipboard. This uses the copy() method on the JTextArea object.

This works perfectly and the clipboard contains the correct text up until the program is closed. Then the clipboard is lost. Is there anyway to have it retain this after the program is quit? The normal operation would be to then quit the program and start it again with the arguments.

This might sound strange but the idea is the user would setup an environment with the GUI and then run it with the arguments in a cron or similar.

问题回答

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

Here s an example that uses the system clipboard. Hope it solves your issue!

Works fine for me when I use Ctrl+C to copy the contents of the text component. So try using the supplied Action (which is what Ctrl+C uses) to do the copy instead of the copy() method:

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

You can use Robot to simulate 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 );

This will definitely stays in the clipboard even after you close your program.

Or you can try

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

which works for me in Windows 7.

I would suggest using the Preferences API for this instead.

For me, this is just enough:

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




相关问题
Spring Properties File

Hi have this j2ee web application developed using spring framework. I have a problem with rendering mnessages in nihongo characters from the properties file. I tried converting the file to ascii using ...

Logging a global ID in multiple components

I have a system which contains multiple applications connected together using JMS and Spring Integration. Messages get sent along a chain of applications. [App A] -> [App B] -> [App C] We set a ...

Java Library Size

If I m given two Java Libraries in Jar format, 1 having no bells and whistles, and the other having lots of them that will mostly go unused.... my question is: How will the larger, mostly unused ...

How to get the Array Class for a given Class in Java?

I have a Class variable that holds a certain type and I need to get a variable that holds the corresponding array class. The best I could come up with is this: Class arrayOfFooClass = java.lang....

SQLite , Derby vs file system

I m working on a Java desktop application that reads and writes from/to different files. I think a better solution would be to replace the file system by a SQLite database. How hard is it to migrate ...

热门标签