Real'sHowTo AddThis Feed Button
Custom Search

Use the System ClipboardTag(s): AWT


import java.awt.datatransfer.*;
import java.awt.*;

public class Java2Clipboard implements ClipboardOwner {
    public static void main(String[] args) throws Exception {
      Java2Clipboard jc = new Java2Clipboard();
      jc.toClipboard();
      Frame f = new Frame
         ("Open a text editor and paste the message from Java");
      f.setSize(600,10);
      f.show();
    }

    public void toClipboard() {
      SecurityManager sm = System.getSecurityManager();
      if (sm != null) {
        try {
           sm.checkSystemClipboardAccess();
           }
        catch (Exception e) {e.printStackTrace();}
        }
      Toolkit tk = Toolkit.getDefaultToolkit();
      StringSelection st = 
           new StringSelection("Hello world from Java");
      Clipboard cp = tk.getSystemClipboard();
      cp.setContents(st, this);
    }

    public void lostOwnership(Clipboard clip, Transferable tr) { 
       System.out.println("Lost Clipboard Ownership?!?");
    }
}
NOTE: You can only use text (no graphic) with this functionality provided by the current JDK (1.4).
blog comments powered by Disqus


If you find this article useful, consider making a small donation
to show your support for this Web site and its content.

Written and compiled by Réal Gagnon ©1998-2014
[ home ]