StringSelection
StringSelection
is a convenience class that can be used for copy and paste operations on Unicode text strings (String
). It implements both the ClipboardOwner
and Transferable
interfaces, so it can be used both as the contents of the clipboard and as its owner. For example, if s
is a StringSelection
, you can call Clipboard.setContents(s,s)
. StringSelection
supports both stringFlavor
and plainTextFlavor
and doesn't do anything when it loses clipboard ownership.
StringSelection Methods
Constructors- public StringSelection(String data)
- The constructor creates an instance of
StringSelection
containingdata
. You can use this object to place the data on a clipboard.
- public DataFlavor[] getTransferDataFlavors()
- The
getTransferDataFlavors()
method returns a two-elementDataFlavor
array consisting ofDataFlavor.stringFlavor
andDataFlavor.plainTextFlavor
. This means that you can paste aStringSelection
as either a JavaString
or as plain text (i.e., the MIME typeplain/text
). - public boolean isDataFlavorSupported(DataFlavor flavor)
- The
isDataFlavorSupported()
method is returnstrue
ifflavor
is eitherDataFlavor.stringFlavor
orDataFlavor.plainTextFlavor
; it returnsfalse
for any other flavor. - public Object getTransferData(DataFlavor flavor) throws UnsupportedFlavorException, IOException
- The
getTransferData()
method returns an object from which you can get the data on the clipboard; the object's type is determined by theflavor
parameter. This method returns aString
containing the data on the clipboard ifflavor
isDataFlavor.stringFlavor
; it returns aStringBufferInputStream
from which you can read the data on the clipboard if you ask forDataFlavor.plainTextFlavor
. Otherwise,getTransferData()
throws anUnsupportedFlavorException
. - public void lostOwnership(Clipboard clipboard, Transferable contents)
- The
lostOwnership()
method ofStringSelection
is an empty stub; it does nothing when you lose ownership. If you want to know when you've lost ownership of string data placed on the clipboard, write a subclass ofStringSelection
and override this method.