SDSU CS 596 Java Programming
Fall Semester, 1998
Cut & Paste Applet
To Applet Index
San Diego State University -- This page last updated 16-Dec-98

Description

This applet shows how to transfer strings to and from the system clipboard.

While done in an applet, this can be done in any Java application. The data transfer classes in JDK 1.1 only supports transfering strings with the system clipboard. You can support transfering other Java objects between Java programs via the system clipboard using serialization.

To test copying to the system clipboard, type some text into the first text area below, then click on the Copy to Clipboard button. This will copy all the text in the text area to the system clipboard. Now, from another application try pasting the contents of your system clipboard. How you do this depends on the platform you are using.

To test pasting from the system clipboard, use another application to copy some text into the system clipboard, then click on the Paste from Clipboard button. This will paste all the text in the system clipboard to the text area.

Two versions of this applet are on the page if you dont feel like using a different a different application to show transfering of text in and out of the system clipboard

Applet

First Text Area
Your browser does not support Java applets, so you can not see this applet operate.
Second Text Area

Applet Tag

<DIV ALIGN="center">
   First Text Area
   <BR>
   <APPLET 
      ARCHIVE="AppletClasses.jar" 
      CODE="TextCutPaste.class" 
      WIDTH=350 HEIGHT=100>
      Your browser does not support Java applets, so
      you can not see this applet operate.
   </APPLET>
   <BR>
   Second Text Area
   <BR><BR>
   <APPLET 
      ARCHIVE="AppletClasses.jar" 
      CODE="TextCutPaste.class" 
      WIDTH=350 HEIGHT=100>
   </APPLET>
</DIV>

Applet Source Code

TextCutPaste.java

import java.applet.*;
import java.awt.event.*;
import java.awt.Button;
import java.awt.TextArea;
import java.awt.Toolkit;
import java.awt.Panel;
import java.awt.BorderLayout;
import java.awt.Graphics;
import java.awt.Dimension;
import java.awt.datatransfer.*;
import java.io.IOException;

public class TextCutPaste extends Applet
  {
  TextArea textDisplay = new TextArea(8, 30);
  Clipboard systemClipBoard = Toolkit.getDefaultToolkit().getSystemClipboard();
  
  public void init( ) 
    {
    setLayout( new BorderLayout() );
    add( BorderLayout.CENTER, textDisplay );
    add( BorderLayout.SOUTH, createButtonPanel() );
    }

  private Panel createButtonPanel()
    {
    Panel buttons = new Panel();

    Button copy = new Button( "Copy to Clipboard" );
    Button paste = new Button( "Paste from Clipboard" );
    buttons.add( copy);
    buttons.add( paste );
    
    copy.addActionListener( new CopyTextToClipboard() );
    paste.addActionListener( new PasteTextFromClipboard() );
    return buttons;
    }
  
  private class CopyTextToClipboard implements ActionListener
    {
    public void actionPerformed( ActionEvent event ) 
      {
      
      String text = textDisplay.getText();
      StringSelection textSelection = new StringSelection( text );
      
      //If your data type supports different flavors, you should support
      // a callback, so the user of the data type can tell you which flavor
      // they want. This is not needed for strings. 
      ClipboardOwner noCallbackNeeded = null;
      systemClipBoard.setContents( textSelection, noCallbackNeeded );
      }
    }
    
  private class PasteTextFromClipboard implements ActionListener
    {
    public void actionPerformed( ActionEvent event ) 
      {
      textDisplay.setText( getStringFromClipboard() );
      }

    private String getStringFromClipboard( )
      {
      String text = "No text transfered";
      
      Transferable selection = systemClipBoard.getContents( this );
      
      if ( selection.isDataFlavorSupported( DataFlavor.stringFlavor ) )
        try
          {
          text = (String) (selection.getTransferData( DataFlavor.stringFlavor ));
          }
        catch ( UnsupportedFlavorException canNotHappen )
          {
          text = "Does not support pasting strings";
          }
        catch (IOException couldHappen )
          {
          text = "IO problem reading clipboard";
          }
      return text;
      }
    }
  }
  


Visitors since 16-Dec-98