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.
| 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 |
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:
| 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:
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