Newsletter sign-up
View all newsletters

Enterprise Java Newsletter
Stay up to date on the latest tutorials and Java community news posted on JavaWorld

Sponsored Links

Optimize with a SATA RAID Storage Solution
Range of capacities as low as $1250 per TB. Ideal if you currently rely on servers/disks/JBODs

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

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

  • Print
  • Feedback
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:

  • Print
  • Feedback

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