import java.awt.datatransfer.Clipboard;
import java.awt.datatransfer.ClipboardOwner;
import java.awt.datatransfer.Transferable;
import java.awt.datatransfer.StringSelection;
import java.awt.datatransfer.DataFlavor;
import java.awt.datatransfer.UnsupportedFlavorException;
import java.awt.Toolkit;
import java.io.*;

public final class TextTransfer implements ClipboardOwner {

  public static void main (String...  aArguments ){
    TextTransfer textTransfer = new TextTransfer();

    //display what is currently on the clipboard
    System.out.println("Clipboard contains:" + textTransfer.getClipboardContents() );

    //change the contents and then re-display
    textTransfer.setClipboardContents("blah, blah, blah");
    System.out.println("Clipboard contains:" + textTransfer.getClipboardContents() );
  }

   /**
   * Empty implementation of the ClipboardOwner interface.
   */
   public void lostOwnership( Clipboard aClipboard, Transferable aContents) {
     //do nothing
   }

  /**
  * Place a String on the clipboard, and make this class the
  * owner of the Clipboard's contents.
  */
  public void setClipboardContents( String aString ){
    StringSelection stringSelection = new StringSelection( aString );
    Clipboard clipboard = Toolkit.getDefaultToolkit().getSystemClipboard();
    clipboard.setContents( stringSelection, this );
  }

  /**
  * Get the String residing on the clipboard.
  *
  * @return any text found on the Clipboard; if none found, return an
  * empty String.
  */
  public String getClipboardContents() {
    String result = "";
    Clipboard clipboard = Toolkit.getDefaultToolkit().getSystemClipboard();
    //odd: the Object param of getContents is not currently used
    Transferable contents = clipboard.getContents(null);
    boolean hasTransferableText =
      (contents != null) &&
      contents.isDataFlavorSupported(DataFlavor.stringFlavor)
    ;
    if ( hasTransferableText ) {
      try {
        result = (String)contents.getTransferData(DataFlavor.stringFlavor);
      }
      catch (UnsupportedFlavorException ex){
        //highly unlikely since we are using a standard DataFlavor
        System.out.println(ex);
        ex.printStackTrace();
      }
      catch (IOException ex) {
        System.out.println(ex);
        ex.printStackTrace();
      }
    }
    return result;
  }
} 



An example run of this class :

>java -cp . TextTransfer
Clipboard contains:Java is in Indonesia.

Clipboard contains:blah, blah, blah

> 










    // If a string is on the system clipboard, this method returns it;
    // otherwise it returns null.
    public static String getClipboard() {
        Transferable t = Toolkit.getDefaultToolkit().getSystemClipboard().getContents(null);
    
        try {
            if (t != null && t.isDataFlavorSupported(DataFlavor.stringFlavor)) {
                String text = (String)t.getTransferData(DataFlavor.stringFlavor);
                return text;
            }
        } catch (UnsupportedFlavorException e) {
        } catch (IOException e) {
        }
        return null;
    }
    
    // This method writes a string to the system clipboard.
    // otherwise it returns null.
    public static void setClipboard(String str) {
        StringSelection ss = new StringSelection(str);
        Toolkit.getDefaultToolkit().getSystemClipboard().setContents(ss, null);
    }







/* clpbrd.inc */
002 
003 public void actionPerformed(ActionEvent event)
004 {
005   Clipboard clip = getToolkit().getSystemClipboard();
006   String cmd = event.getActionCommand();
007   if (cmd.equals("Kopieren")) {
008     String s = "Es ist " + System.currentTimeMillis() + "Uhr";
009     StringSelection cont = new StringSelection(s);
010     clip.setContents(cont, this);
011   } else if (cmd.equals("Einfuegen")) {
012     Transferable cont = clip.getContents(this);
013     if (cont == null) {
014       System.out.println("Zwischenablage ist leer");
015     } else {
016       try {
017         String s = (String) cont.getTransferData(
018           DataFlavor.stringFlavor
019         );
020         System.out.println(s);
021       } catch (Exception e) {
022         System.out.println(
023           "Zwischenablage enthält keinen Text"
024         );
025       }
026     }
027   }
028 }
029 
030 public void lostOwnership(Clipboard clip, Transferable cont)
031 {
032   System.out.println("Inhalt der Zwischenablage ersetzt");
033 }