You can use this to make a text converter i.e. to change the contents you have copied into something different.
Like you can make a software which converts all copied text into uppercase letter or vice-versa, so that if you have written some data in lowercase by mistake and you want to convert the data into uppercase and just copy the lowercase data and run the program and then paste it. The data you copied has been converted to uppercase.
Now first we include all the import files.
import java.awt.Toolkit; import java.awt.datatransfer.DataFlavor; import java.awt.datatransfer.StringSelection; import java.awt.datatransfer.Transferable;
Here, ToolKit is used to obtain the data from the clipboard, StringSelection is for storing the updated text to be placed at the clipboard, DataFlavor objects are constant and never change once instantiated They represent the data format.
Transferable can be used to provide data for a transfer operation.
Now we define the class and the main method:
public class ClipBoard {
public static void main(String args[])
{
Transferable t = Toolkit.getDefaultToolkit().getSystemClipboard().getContents(null);
try {
if (t != null && t.isDataFlavorSupported(DataFlavor.stringFlavor)) {
String text = (String)t.getTransferData(DataFlavor.stringFlavor);
text=text.toUpperCase();
StringSelection ss = new StringSelection(text);
Toolkit.getDefaultToolkit().getSystemClipboard().setContents(ss, null);
}
} catch (Exception e) {
}
}
}
Here, We defined the name of the class as ClipBoard. We defined the main method and we make use of Toolkit to obtain the contents from the clipboard and store them in the transferable object.
Now we check if the transferable object is not null, because if its null then the clipboard is empty. We use the DataFlavor.stringFlavor to know whether the data is in string format or not. We check whether our transferable object support the stringFlavor.
Now we obtain the clipboard data from the Transferable object making use of getTransferData(DataFlavor.stringFlavor) and this will get the data from the clipboard and now we may store it in a variable called text.
Now we may do any changes in the text which we want to reflect in the clipboard.
Here I have converted the data obtained in the uppercase. Now we use the StringSelection to store the modified string, we make use if the toolkit to place this modified string in the clipboard.
Now if the data you copied was abc then after this program run the data will become ABC.
Suppose i copied a text say “i am happy”.
Now after the program run it will become
I AM HAPPY
import java.awt.Toolkit;
import java.awt.datatransfer.DataFlavor;
import java.awt.datatransfer.StringSelection;
import java.awt.datatransfer.Transferable;
public class ClipBoard {
public static void main(String args[])
{
Transferable t = Toolkit.getDefaultToolkit().getSystemClipboard().getContents(null);
try {
if (t != null && t.isDataFlavorSupported(DataFlavor.stringFlavor)) {
String text = (String)t.getTransferData(DataFlavor.stringFlavor);
text=text.toUpperCase();
StringSelection ss = new StringSelection(text);
Toolkit.getDefaultToolkit().getSystemClipboard().setContents(ss, null);
}
} catch (Exception e) {
}
}
}







See the prices for this post in Mr.Bool Credits System below: