Newsletter sign-up
View all newsletters

Sign up for our Enterprise Java Newsletter

Enterprise Java

Java Tip 61: Cut, copy, and paste in Java

Learn the inner workings of the clipboard and how to transfer data in Java

  • Digg
  • Reddit
  • SlashDot
  • Stumble
  • del.icio.us
  • Technorati
  • dzone
This article will give you a good understanding of how to send and get information from the clipboard in Java. You'll also learn how to deal with the different data flavors available. Finally, we'll cover the multiple personalities of clipboards and how they provide support for more than one data flavor.

Java offers two types of clipboards: local and system. Local clipboards are only available inside the virtual machine that your applet or application is running. However, unlike some operating systems that limit you to only one clipboard, Java allows you to have as many local clipboards as you desire. Accessing a particular local clipboard is as easy as referring to it by name.

System clipboards are directly linked with the peer operating system, allowing your application to transfer information among any applications running under that operating system. One disadvantage of using the system clipboard is that you can only transfer text data. Other types of objects are not supported by the system clipboard. With any luck, this issue will be addressed in the next release of the JDK.

Before we go any further, let's take a look at all the classes involved in manipulating the clipboard. These classes, listed in the table below, are all part of the java.awt.datatransfer package.

List of all classes in the java.awt.datatransfer package
Name Type Description
Clipboard Class Deals with everything that is a transferable
ClipboardOwner Interface Every class that deals with the clipboard must implement this interface. This interface is used to notify when the data originally placed in the clipboard has been overwritten
Dataflavor Class Represents all the data types that transferable support
StringSelection Class One type of transferable that is supplied with Java
Transferable Interface Wrapper to objects passed to the clipboard
UnsupportedFlavor</ code> Exception Class Exception thrown by transferable for an unsupported data flavor


More on the clipboard classes

Let's go deeper into our exploration of the java.awt.datatransfer package by looking in detail at each class.

The Clipboard class
The Clipboard class is your link to accessing the clipboard. It includes three methods, which are defined in the following table:

Clipboard class
Method Description
String getName () Get the name of the clipboard
void setContents (Transferable, ClipboardOwner) Set the content of the clipboard along with owner object
Transferable getContent (Object) Get the content of the clipboard in the form of a Transferable object. The object passed as a parameter is the owner


The three Clipboard class methods above allow you to name the clipboard, send information to it, or get information from it. Accessing the system clipboard or creating a local clipboard is different and requires a bit more discussion. To access the system clipboard, assign a reference from the system clipboard to the Clipboard class, such as:

Clipboard clipboard = getToolkit ().getSystemClipboard ();

On the other hand, to create a local clipboard you only need to create a Clipboard object with the name that you want to assign to it, for example:

  • Digg
  • Reddit
  • SlashDot
  • Stumble
  • del.icio.us
  • Technorati
  • dzone
Comments (2)
Login
Forgot your account info?

website with next page... :(By Anonymous on October 12, 2009, 9:12 amwebsite with next page... :(

Reply | Read entire comment

Conditional DnD / Cut Copy Paste based on same class but with different stateBy Ali3 on August 28, 2009, 11:48 pmThanks for the fantastic tips. The only question I have left is how can I add logic to prevent an object from being dropped under some circumstances based on the...

Reply | Read entire comment

View all comments

Add comment
Anonymous comments subject to approval. Register here for member benefits.
Have a JavaWorld account? Log in here. Register now for a free account.
Resources
  • For a discussion of the MIME standard, see
    http://andrew2.andrew.cmu.edu/rfc/rfc2046.html
  • For more on transferring data in Java, see JavaWorld's How-To column, "Unfurling Java data transfer" (May 1998)
    http://www.javaworld.com/jw-05-1998/jw-05-howto.html
  • Graphic Java, Mastering the AWT by David M. Geary, Second Edition, Prentice Hall
  • Migrating from Java 1.0 to Java 1.1 by Daniel I. Oshi and Parel A. Vorobiev PhD, Ventana Publications
  • Java AWT Reference (Version 1.1) by John Zukowski, O'Reilly
  • Java 1.1 Developer's Handbook by Philip Heller and Simon Roberts, Sybex
  • The Java developer ALMANAC 1998 by Patrick Chan, Addison Wesley