Java Reference
In-Depth Information
•
boolean isDataFlavorSupported(DataFlavor flavor)
indic-
ateswhetherornotthespecifiedflavorissupported;truereturnswhen
flavor
is supported.
Each time that you invoke
setContents()
, the object passed to
owner
is the
owner of the clipboard content. If you call this method with a different owner, AWT
notifies the previous owner that it's no longer the owner (some other content is on the
clipboard) by calling
ClipboardOwner
's
void lostOwnership(Clipboard
clipboard, Transferable contents)
method.
Because users typically want to copy, cut, and paste text,
java.awt.datatransfer
provides
StringSelection
as an implementation
of
Transferable
and
ClipboardOwner
(
lostOwnership()
is left empty;
youmustsubclass
StringSelection
andoverride
lostOwnership()
whenyou
need this notification). You would use
StringSelection
to transfer strings to and
from a clipboard.
Thefollowingexamplepresents
copy()
,
cut()
,and
paste()
methodsthatshow
you how to perform copy, cut, and paste operations in the context of the
TextArea
class.Theexamplespecifiesa
ta
variablethatreferencesa
TextArea
instance,anda
clipboard
variable that references a
Clipboard
instance:
void copy()
{
StringSelection
ss
=
new
StringSelec-
tion(ta.getSelectedText());
clipboard.setContents(ss, ss);
}
void cut()
{
copy();
ta.replaceRange("",
ta.getSelectionStart(),
ta.getSelectionEnd());
}
void paste()
{
Transferable clipData = clipboard.getContents(this);
if (clipData != null)
try
{