Question: I have been trying for a month now to reliably set the system clipboard in my program. Currently it works about 95/100 times. But I keep searching. 

Yesterday I came upon this [the Java DefaultEditorKit.copyAction](http://docs.oracle.com/javase/1.4.2/docs/api/javax/swing/text/DefaultEditorKit.CopyAction.html) and died a little inside seeing there was already something written that might do what I want. 

Though the problem is that this is a "Action" for a dialog?

How can I issue the text I want copied to the clipboard? I do not wish to attach this "Action" to any button/component in my app. I want to be able to do 

```
DefaultEditorKit.copyAction("Put this on Clipboard");

```

But this is undefined. I am not sure how to trigger this "action" and give it some text to work with?

EDIT: Here is my code that causes an exception.

```
public void setClip2(String arg)
{
    while(true) 
    {
        try
        {
            sysClip.setContents(new StringSelection(arg), null);
        }
        catch(Exception e)
        {
            try {Thread.sleep(20);} catch (InterruptedException e1) {}
            continue;
        }
        break;
    }
    return;
}

```

Answer: 
> 
> I just wondered if there was a way to reliably set the clipboard. (this method fails if you do not wait long enough trying to set it, which is usually about 1-2 seconds
> 
> 
> 

Not sure why you have to wait to set the contents of the clipboard.

This program doesn't have any problem refreshing the clipboard every 200ms. That is the number increments by 1 every time is it displayed as expected:

```
import java.awt.*;
import java.awt.datatransfer.*;
import java.io.*;

class ClipboardLoopTest
{
    public static void main(String[] args)
        throws InterruptedException
    {
        for (int i = 0; i < 100; i++)
        {
            //  add data to clipboard

            try
            {
                Clipboard c = Toolkit.getDefaultToolkit().getSystemClipboard();
                StringSelection testData;
                testData = new StringSelection( "Test: " + i );
                c.setContents(testData, testData);

                //  Get clipboard contents, as a String

                Transferable t = c.getContents( null );

                if ( t.isDataFlavorSupported(DataFlavor.stringFlavor) )
                {
                    Object o = t.getTransferData( DataFlavor.stringFlavor );
                    String data = (String)t.getTransferData( DataFlavor.stringFlavor );
                    System.out.println( "Clipboard contents: " + data );
                }
            }
            catch(Exception e)
            {
                System.out.println(e);
            }

            Thread.sleep(200);
        }

        System.exit(0);
    }
}

```

I'm using JDK 7 on Windows 7.

Maybe you can post your SSCCE that demonstrates the problem.
`DefaultEditorKit.copyAction` actually use (through some layers) the functionnality in java.awt.datatransfer. There you will find classes to send data to the clipboard.

Basically, if you just want to send a string to the clipboard without using any Swing component, you setup a `ClipboardOwner`, you create a `StringSelection` object and you give it to the system clipboard. Here is a most basic example:

```
Clipboard clipboard = Toolkit.getDefaultToolkit().getSystemClipboard();
clipboard.setContents( new StringSelection("Put this on Clipboard"), new ClipboardOwner() {

    @Override
    public void lostOwnership(Clipboard clipboard, Transferable contents) {
      System.out.println("I am no longer the clipboard owner.");
    }
} );

```