Placing text on the computer clipboard

It is very easy to place text on the clipboard with Java.
We just get an instance of the default toolkit and from that we get hold of a reference to the clipboard object.
To place some text on the clipboard we need to pass an object of type Transferable and another object which is the clipboard owner to the method setContents.
To make it easy we may specify null as the owner, and as the first argument we pass an instance of the class StringSelection which implements the Transferable interface.
To test the code, just run it and then go to some text editor of your choice and select paste.

Search for more Java info on this site here:
Custom Search

Please type any questions here.