Class StringSelection in Java

digitization, transformation, binary

Class StringSelection in Java

StringSelection class is a convenience class implements the Transferable and ClipboardOwner interfaces in order to make it very easy to transfer String values through the AWT data transfer mechanism.

It is able to transfer String values using either the DataFlavor.stringFlavor or DataFlavor.plainTextFlavor data flavors.

Ads code goes here

To create a StringSelection object, we need to simply pass the String we want to transfer to the StringSelection() constructor. We can then make the StringSelection available for transfer by passing it to the setContents() method of the Clipboard. You need never call the methods of StringSelection explicitly.

StringSelection is a “convenience” class that can be used for copy and paste operations on Unicode text strings. For example, we could place a string on the system’s clipboard with the following code:

Clipboard clip =Toolkit.getDefaultToolkit().getSystemClipboard();
StringSelection sel = new StringSelection("Test cut and paste.");

The class structure of StringSelection class is given as

public class java.awt.datatransfer.StringSelection extends java.lang.Object implements java.awt.datatransfer.Transferable,java.awt.datatransfer.ClipboardOwner{
// Public Constructor
public StringSelection(String data);
// Public Instance Methods
public synchronized Object getTransferData(DataFlavor flavor)
public synchronized Object getTransferData'u'throws UnsupportedFlavorException, IOException;
public synchronized Object getTransferData'u'// From Transferable
public synchronized DataFlavor[] getTransferDataFlavors(); // From Transferable
public boolean isDataFlavorSupported(DataFlavor flavor); // From Transferable
public void lostOwnership(Clipboard clipboard, Transferable contents);// From ClipboardOwner

The details of the class structure are given as follows:

public StringSelection(String data);

public StringSelection(String data) constructs a StringSelection object from the given string.

data – The string to be placed in a clipboard.

public synchronized Object getTransferData (DataFlavor flavor);

public synchronized Object getTransferData (DataFlavor flavor) method returns the string this StringSelection represents. This is returned either as a String object or a Reader object, depending on the flavor requested.

READ  Class FileOutputStream in Java

This method returns the string that the StringSelection was constructed with. This is returned either as a String object or a Reader object, depending on the flavor requested.

flavor – The requested flavor for the returned data, which can be either DataFlavor.stringFlavor or DataFlavor.plainTextFlavor.

public synchronized DataFlavor[] getTransferDataFlavors();

public synchronized DataFlavor[] getTransferDataFlavors() method returns DataFlavor.stringFlavor and DataFlavor.plainTextFlavor.

This method returns an array of the data flavors the StringSelection supports.

public boolean isDataFlavorSupported (DataFlavor flavor);

public boolean isDataFlavorSupported (DataFlavor flavor) method returns true if flavor is supported; false otherwise.

This method returns true if flavor is supported; false otherwise.

flavor – The flavor in question.

public void lostOwnership (Clipboard clipboard, Transferable contents);

public void lostOwnership (Clipboard clipboard, Transferable contents) method actually does nothing.

clipboard – The clipboard whose contents are changing.
contents – The contents that were on the clipboard.

Share and Enjoy !

Leave a Comment

Your email address will not be published. Required fields are marked *