import java.awt.Component;
import java.awt.Toolkit;
import java.awt.datatransfer.Clipboard;
import java.awt.datatransfer.ClipboardOwner;
import java.awt.datatransfer.DataFlavor;
import java.awt.datatransfer.StringSelection;
import java.awt.datatransfer.Transferable;
import java.awt.datatransfer.UnsupportedFlavorException;
import java.io.IOException;

class MyClass implements ClipboardOwner
   {
   //...

   /**
   * handle to the system clipboard for cut/paste,
   */
   Clipboard clipboard;

   /**
   * get current contents of the Clipboard as a String.
   * Returns null if any trouble.
   */
   String getClip()
      {
      try
         {
         try
            {
            Transferable contents = clipboard.getContents( this );
            // stringFlavor is when you want an ordinary String.
            // plainTextFlavor is for when you want a StringReader
            // instead of a String
            if ( contents.isDataFlavorSupported ( DataFlavor.stringFlavor ) )
               {
               return( String ) contents.getTransferData ( DataFlavor.stringFlavor );
               }
            }
         catch ( UnsupportedFlavorException e )
            {
            }
         }
      catch ( IOException e )
         {
         }
      return null;
      } // end getClip

   /**
   * Copy data into the Clipboard. We become its owner
   * until somebody else changes the value.
   */
   void setClip( String s )
      {
      if ( s != null )
         {
         StringSelection contents = new StringSelection (s);
         clipboard.setContents(contents, this);
         }
      } // end setClip

   /**
   * If we put data in the Clipboard, and somebody else
   * changes the clipboard, we lose ownership and are notified
   * here.
   * Satisfies the requirement of this class
   * to implement java.awt.datatransfer.ClipboardOwner.
   */
   public void lostOwnership( Clipboard clipboard , Transferable contents )
      {
      }
   /**
   * initialises hook to the system clipboard
   */
   void initClipboardHook( Component owner )
      {
      if ( clipboard == null )
         {
         Toolkit t = owner.getToolkit();
         if ( t != null )
            {
            clipboard = t.getSystemClipboard();
            } // end if
         } // end if
      } // end initClipBoard
   } // end class MyClass

