Java Reference
In-Depth Information
if
(clipData.isDataFlavorSupported(DataFlavor.stringFlavor))
{
String
text
=
(String)
clipData.getTransferData(DataFlavor.stringFlavor);
ta.replaceRange(text, ta.getSelectionStart(),
ta.getSelectionEnd());
}
}
catch (UnsupportedFlavorException ufe)
{
ta.setText("flavor not supported");
}
catch (IOException ioe)
{
ta.setText("no data to paste");
}
}
copy() 'sfirsttaskistoextracttheselectedtextfromthetextareabycalling Tex-
tComponent 's String getSelectedText() method.Itthenpassesthistextto
the StringSelection(String data) constructortocreateatransferableobject
that contains this text.
Continuing, copy() passes this object to the clipboard by invoking clip-
board.setContents(ss, ss) . The same StringSelection object ( ss ) is
passedasthetransferableobjectandtheclipboardownerbecause StringSelection
implements Transferable and ClipboardOwner .
cut() is much simpler. This method first invokes copy() to copy the selected
texttotheclipboard.Ittheninvokes TextArea 's void replaceRange(String
str, int start, int end) methodtoremovetheselectedtext(delimitedbythe
integer values returned from TextComponent 's int getSelectionStart()
and int getSelectionEnd() methods) by replacing it with the empty string.
paste() isthemostcomplexofthethreemethods.Itfirstinvokes Clipboard 's
Transferable getContents(Object requestor) methodtoreturnatrans-
ferableobjectrepresentingthecurrentcontentsoftheclipboard(ornullwhentheclip-
boardisempty).The requestor parameteriscurrentlynotused;itmaybeimplemen-
ted in a future release of the java.awt.datatransfer package.
Search WWH ::




Custom Search