The following code from a standalone application works in ubuntu:
import java.awt.Toolkit; import java.awt.datatransfer.Clipboard; import java.awt.datatransfer.DataFlavor; import java.awt.datatransfer.StringSelection; import java.awt.datatransfer.UnsupportedFlavorException; import java.io.IOException; public class ClipboardTest { public static void main(String[] args) throws Exception { Clipboard clipBoard = Toolkit.getDefaultToolkit().getSystemClipboard(); // print the last copied thing System.out.println(clipBoard.getContents(null).getTransferData(DataFlavor.stringFlavor)); StringSelection data = new StringSelection("NOW"); clipBoard.setContents(data, data); // prints NOW System.out.println(clipBoard.getContents(null).getTransferData(DataFlavor.stringFlavor)); } }
Pasting (Ctrl+V) into a different application results in nothing; I expect "NOW". Calling the above code a second time gives the following exception:
Exception in thread "main" java.awt.datatransfer.UnsupportedFlavorException: Unicode String at sun.awt.datatransfer.ClipboardTransferable.getTransferData(ClipboardTransferable.java:160)
As a standalone application, this should work even after 2011 security changes. Copying via Ctrl+C from inside of a JTextField and then pasting elsewhere works.
Have been unsuccessful on ubuntu 11.04 with both the latest java7 (jdk1.7.0_10) and jdk1.6.0_33; It should work and does work as expected on windows 7 with the latest java7 and on mac osx 10.6 with java6_37. Also tried xubuntu 12.04 with those javas and it doesn't work there. Is this a linux/ubuntu bug?
Related question
Select "File -> Exit" from the Policy Tool window to close the policy editor. Restart your browser. You should now be able to copy and paste between Java Swing applets and other computer applications such as Microsoft Excel. Please note to use Ctrl-C and Ctrl-V to copy and paste.
A class that implements a mechanism to transfer data using cut/copy/paste operations. FlavorListener s may be registered on an instance of the Clipboard class to be notified about changes to the set of DataFlavor s available on this clipboard (see addFlavorListener(java. awt.
I got the same problem with the application at my work and here's an article I've found that explain why and possible solutions. I hope it helps.
Why it happens
Clipboard persistence is a bug that affects many programs under Ubuntu and other X11-based operating systems. Fixing it is a Google Summer of Code 2010 project. Wikipedia has a good overview of the issue. If you want to fix as a user, you can install Parcellite or another clipboard manager. If you want to fix it as a programmer, you can modify your program to conform to the ClipboardManager specification.
X-Window wiki
Using gnome library you could call the store method on the clipboard and fix this. That's the only thing so far that seems to be worth trying. Also saw a similar thing for GTK but only in an Eclipse's bug.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With