Free Online Courses for Software Developers - MrBool
× Please, log in to give us a feedback. Click here to login
×

You must be logged to download. Click here to login

×

MrBool is totally free and you can help us to help the Developers Community around the world

Yes, I'd like to help the MrBool and the Developers Community before download

No, I'd like to download without make the donation

×

MrBool is totally free and you can help us to help the Developers Community around the world

Yes, I'd like to help the MrBool and the Developers Community before download

No, I'd like to download without make the donation

Manipulating Clipboard content with Java

In this article you will learn how to make a program that helps you to change the contents of your clipboard using a simple java program.

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.

Listing1: Including 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:

Listing2: Clas 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.

Output:

Suppose i copied a text say “i am happy”.
Now after the program run it will become
I AM HAPPY

Full source code:


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) {
	    } 
	}
	}


My main area of specialization is Java and J2EE. I have worked on many international projects like Recorders,Websites,Crawlers etc.Also i am an Oracle Certified java professional as well as DB2 certified

What did you think of this post?
Services
[Close]
To have full access to this post (or download the associated files) you must have MrBool Credits.

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

Individually – in this case the price for this post is US$ 0,00 (Buy it now)
in this case you will buy only this video by paying the full price with no discount.

Package of 10 credits - in this case the price for this post is US$ 0,00
This subscription is ideal if you want to download few videos. In this plan you will receive a discount of 50% in each video. Subscribe for this package!

Package of 50 credits – in this case the price for this post is US$ 0,00
This subscription is ideal if you want to download several videos. In this plan you will receive a discount of 83% in each video. Subscribe for this package!


> More info about MrBool Credits
[Close]
You must be logged to download.

Click here to login