Pasting onto the clipboard

H

Hari

In java, I know in the 1.4.2 API specification there is a Clipboard class
and a Clipboard owner class. I tried but couldn't find out how you just
paste a String onto the clipboard. Could someone show me the code I need to
copy a simple String onto the clipboard which can be used in other
mainstream applications (like Word, etc.)?
 
S

Silas Snider

In java, I know in the 1.4.2 API specification there is a Clipboard
class and a Clipboard owner class. I tried but couldn't find out how
you just paste a String onto the clipboard. Could someone show me the
code I need to copy a simple String onto the clipboard which can be
used in other mainstream applications (like Word, etc.)?

Use something like (excluding neccessary try/catch blocks)

StringSelection s = new StringSelection(myString);
Toolkit.getSystemClipboard().setContents(s,s); //guessing on the second
//parameter, but the api says that StringSelection implements
//ClipboardOwner, so I am assuming its right.
 
H

Hari

Thanks a lot,

I didn't look in the java.awt.Toolkit, sorry for bothering me. You didn't
have to type out the code, just direct me to the Toolkit class. thanks alot.
It all worked. For future reference, do you know how to put an image on the
clipboard or retreive contents from the clipboard? Thanks a lot.
 
S

Silas Snider

Thanks a lot,

I didn't look in the java.awt.Toolkit, sorry for bothering me. You
didn't have to type out the code, just direct me to the Toolkit class.
thanks alot. It all worked. For future reference, do you know how to
put an image on the clipboard or retreive contents from the clipboard?
Thanks a lot.

I believe you will have to write your own class that implements
Transferable, as there is an ImageFlavor for Transferables to use, but
there is no Transferable I can find that uses it.
 
T

Tor Iver Wilhelmsen

Hari said:
It all worked. For future reference, do you know how to put an image on the
clipboard or retreive contents from the clipboard? Thanks a lot.

You get the contents using getContents() on the Clipboard. This gets
you a Transferable which you can ask for data of a given "flavor".

Images are a special flavor (supported by DataFlavor.imageFlavor); if
you have an java.awt.Image object, you should be able to send it to
the Clipboard, but there doesn't seem to be any default Transferable
or ClipboardOwner classes to be used in the standard APIs. This means
it's easy to get an image FROM the clipboard in Java, but hard to PUT
one there.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top