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

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

RELATED TOPICS

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
NameTypeDescription
ClipboardClassDeals with everything that is a transferable
ClipboardOwnerInterfaceEvery 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
DataflavorClassRepresents all the data types that transferable support
StringSelectionClassOne type of transferable that is supplied with Java
TransferableInterfaceWrapper to objects passed to the clipboard
UnsupportedFlavor</ code> ExceptionClassException thrown by transferable for an unsupported data flavor
RELATED TOPICS
1 2 Page
View Comments
Join the discussion
Be the first to comment on this article. Our Commenting Policies