Java Reference
In-Depth Information
Programmers use the Clipboard class to declare a non-primitive variable
that will store the clipboard contents. Then, using methods from the Toolkit
class and the Transferable interface, programmers write code to perform the cut,
copy, and paste operations. The Toolkit class is the superclass of all actual imple-
mentations of the Abstract Window Toolkit. It is used to bind the various com-
ponents to particular native , or system-dependent, implementations, such as the
Clipboard. The Transferable interface allows for methods that can be used to
provide data for a transfer operation. A Transferable object resides in a buffer
between the clipboard and the calling application.
Table 6-5 displays some of the methods from the Clipboard, Toolkit, and
Transferable interfaces, along with their purpose and sample code. These meth-
ods can be used to obtain information from the operating system, such as
desktop properties or print-job information, as well as data from the clipboard.
Table 6-5
Methods Associated with the Clipboard
METHOD
PURPOSE
EXAMPLE
getDefaultToolkit()
Obtains the system toolkit if it exists;
Toolkit myTools =
however, it usually is combined with
Toolkit.getDefaultToolkit();
another method.
getSystemClipboard()
Gets the most recent value of the system
Clipboard cb =
clipboard provided by the native platform.
myTools.getSystemClipboard();
getContents()
Returns a transferable object representing
Transferable fromCb =
the current contents of the clipboard.
cb.getContents(this);
setContents()
Sets the current contents of the clipboard
Transferable toCb =
to the specified transferable object.
cb.setContents();
getTransferData()
Returns an object, which represents the
String s = (String)
data to be transferred. The class of the
content.getTransferData
object returned is defined by the
(DataFlavor.stringFlavor);
representation class of the flavor.
getDesktopProperty()
Obtains a value for the specified desktop
String currentFont =
property.
getDesktopProperty("win.menu.font");
getImage()
Obtains an image file from the operating
I mage icon =
system file structure.
Toolkit.getDefaultToolkit().getImage
("calcImage.gif");
The getTransferData() method requires a DataFlavor argument that encap-
sulates information about specific data formats. In the example in the table, the
flavor is stringFlavor , a static variable in Java representing any generic string of
characters. Thus, when the getTransferData(DataFlavor.stringFlavor) statement
is executed, any string of characters can be transferred from the buffer. The
result usually is cast to be a String so that it can display in TextField or TextArea
components.
 
Search WWH ::




Custom Search