Re: Image to ClipBoard (linux)
On 02 Jun 2011 22:30:55 GMT, ricoh51 <ricoh51@free.fr> wrote:
On Thu, 02 Jun 2011 23:05:33 +0100, rossum wrote:
On 02 Jun 2011 19:35:01 GMT, ricoh51 <ricoh51@free.fr> wrote:
public void copyToClipboard(){
Clipboard clipboard = Toolkit.getDefaultToolkit
().getSystemClipboard();
ImageTransferable selection = new ImageTransferable
(image);
clipboard.setContents(selection, null);
I am not sure if it is causing your problem, but in my clipboard code I
set the owner parameter here to 'this', not to 'null'.
but setContents needs a :
java.awt.datatransfer.Transferable,java.awt.datatransfer.ClipboardOwner
and "this" is not a ClipboardOwner in my example.
Do you have a minimal example?
Best Regards
eric
What I have is a very simple utility class that I use to protect
myself from the full horror of the Java clipboard. It is text only
though, not images. If you get yours working I will probably add
that.
Careful with the line wrap, this is as written and not reformatted for
usenet.
rossum
// *** Code starts ***
import java.awt.Toolkit;
import java.awt.datatransfer.Clipboard;
import java.awt.datatransfer.ClipboardOwner;
import java.awt.datatransfer.DataFlavor;
import java.awt.datatransfer.StringSelection;
import java.awt.datatransfer.Transferable;
import java.awt.datatransfer.UnsupportedFlavorException;
import java.io.IOException;
/**
* a simple interface with the Windows clipboard.
*
* @author Martin Ross
*/
public class MyClipBoard implements ClipboardOwner {
public MyClipBoard() {}
/**
* no action taken or needed. Required as part of the
ClipboardOwner Interface.
*/
public void lostOwnership(Clipboard clipboard, Transferable
contents) {
// No action required.
}
/**
* adds some text to the clipboard.
*
* @param text the text to copy to the clipboard.
*/
public void setText(String text) {
StringSelection ssText = new StringSelection(text);
Clipboard clipboard =
Toolkit.getDefaultToolkit().getSystemClipboard();
clipboard.setContents(ssText, this);
} // end setText()
/**
* clears the clipboard.
*/
public void clear() {
Clipboard clipboard =
Toolkit.getDefaultToolkit().getSystemClipboard();
clipboard.setContents(null, this);
} // end clear()
/**
* retrieves text from the clipboard.
*
* @return the text retrieved from the clipboard or the empty
string.
*
* @throws java.lang.IOException if the clipboard holds the wrong
type of data.
* @throws java.lang.IOException if there was an error accessing
the clipboard.
*/
public String getText() throws IOException {
String result = "";
Clipboard clipboard =
Toolkit.getDefaultToolkit().getSystemClipboard();
Transferable contents = clipboard.getContents(null);
boolean hasTransferableText = (contents != null) &&
contents.isDataFlavorSupported(DataFlavor.stringFlavor);
if ( hasTransferableText ) {
try {
result = (String)
contents.getTransferData(DataFlavor.stringFlavor);
} catch (UnsupportedFlavorException ufe) {
throw new IOException("MyClipBoard.getText: text data
not supported.", ufe);
} catch (IOException ioe) {
throw new IOException("MyClipBoard.getText: clipboard
unavailable.", ioe);
} // end try/catch
} // end if
return result;
} // end getText()
} // end class MyClipBoard