Java - Clipboard copied content disappear after program exits
I would like to replace the system clipboard content with my java code, but the copied content disappears once the java p开发者_开发知识库rogram exits. How can I make the data persistent?
Thanks a lot.
Clipboard clipboard = Toolkit.getDefaultToolkit().getSystemClipboard();
Transferable t = clipboard.getContents(null);
if (t.isDataFlavorSupported(DataFlavor.stringFlavor))
{
String data = (String) t.getTransferData(DataFlavor.stringFlavor);
System.out.println(data);
}
StringSelection stringSelection = new StringSelection("Replaced Text");
clipboard.setContents(stringSelection, null);
// just to keep program running...
Scanner scanner = new Scanner(System.in);
scanner.nextLine();
edit: I work on Ubuntu 10.10, with java version 1.6_20.
It seems that the one that (your program) put the clipboard content on the clipboard must be able to serv it in Ubuntu 10.10.
Installing glipper (sudo apt-get install glipper
) works for me. It manages your clipboard. (It's an applet so it seems that it must be added to your panel to start.)
精彩评论