Download this source file


// Clipboard Example - Shows how to access the global clipboard from Java2.
// Note that a proper implementation would disable the copy button unless
// something was selected, and that the paste button would be disabled
// unless something was on the clipboard that could be pasted (i.e., text).
// Note too that clipboard access is restricted from Applets unless extra
// permissions are granted by the user.
//
// Adopted by Wayne Pollock, Tampa Florida USA 2002, from an article
// appearing in the "Java Developer Connection ("developer.java.sun.com/developer/"),
// "New Data Transfer Capabilities" by John Zukowski, November 2001.

import java.awt.*;
import java.awt.event.*;
import java.awt.datatransfer.*;
import javax.swing.*;

public class ClipEx
{
   public static void main ( String [] args )
   {
      JFrame frame = new JFrame( "Clipboard Copy/Paste Example" );
      Container c = frame.getContentPane();

      final Toolkit kit = Toolkit.getDefaultToolkit();
      final Clipboard clipboard = kit.getSystemClipboard();
      final JTextArea jta = new JTextArea();
      JScrollPane spane = new JScrollPane( jta );
      c.add( spane, BorderLayout.CENTER ); 
      JPanel bot = new JPanel();
        JButton copyBtn = new JButton( "Copy" );
        bot.add( copyBtn );

      copyBtn.addActionListener( new ActionListener ()
        {
          public void actionPerformed( ActionEvent ae )
          {
             String selection = jta.getSelectedText();
             jta.requestFocus();
             if ( selection == null )
                return;  // Nothing to copy so just return
             StringSelection data = new StringSelection( selection );
             clipboard.setContents( data, data );
          }
        }
      );

      JButton paste = new JButton( "Paste" );
      bot.add( paste );

      paste.addActionListener( new ActionListener()
        {
          public void actionPerformed( ActionEvent ae )
          {
            Transferable clipData = clipboard.getContents( clipboard );
            jta.requestFocus();
            if ( clipData != null )
            {
              try
              {
                if (clipData.isDataFlavorSupported ( DataFlavor.stringFlavor))
                {
                   String s = (String)
                     ( clipData.getTransferData( DataFlavor.stringFlavor ) );
                   jta.replaceSelection( s );
                } else {
                  kit.beep();
                }
              } catch ( Exception e )
              {
                 System.err.println( "Problems getting data: " + e );
              }
            }
          }
        }
      );

      c.add( bot, BorderLayout.SOUTH );

      frame.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );
      frame.setSize( 400, 250 );

      // Center the Frame on the screen:
      int screenWidth = kit.getScreenSize().width;
      int screenHeight = kit.getScreenSize().height;
      int frameWidth = frame.getWidth();
      int frameHeight = frame.getHeight();
      frame.setLocation( ( screenWidth - frameWidth ) / 2,
         ( screenHeight - frameHeight ) / 2 );

      frame.show();
   }
}




Send comments and mail to the WebMaster.