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
StringSelectioncontainingdata. You can use this object to place the data on a clipboard.
- public DataFlavor[] getTransferDataFlavors()
- The
getTransferDataFlavors()method returns a two-elementDataFlavorarray consisting ofDataFlavor.stringFlavorandDataFlavor.plainTextFlavor. This means that you can paste aStringSelectionas either a JavaStringor as plain text (i.e., the MIME typeplain/text). - public boolean isDataFlavorSupported(DataFlavor flavor)
- The
isDataFlavorSupported()method is returnstrueifflavoris eitherDataFlavor.stringFlavororDataFlavor.plainTextFlavor; it returnsfalsefor 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 theflavorparameter. This method returns aStringcontaining the data on the clipboard ifflavorisDataFlavor.stringFlavor; it returns aStringBufferInputStreamfrom 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 ofStringSelectionis 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 ofStringSelectionand override this method.