Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Image to ClipBoard (linux)

134 views
Skip to first unread message

ricoh51

unread,
Jun 2, 2011, 3:35:01 PM6/2/11
to
Hi all,
I have problems to place an image on the clipboard (ubuntu 10.10). The
code is below, and for example Gimp says that there is nothing in the
clipboard!

import java.awt.*;
import java.awt.datatransfer.Clipboard;
import java.awt.datatransfer.DataFlavor;
import java.awt.datatransfer.Transferable;
import java.awt.datatransfer.UnsupportedFlavorException;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.image.BufferedImage;
import javax.swing.*;

public class testClipboard extends JPanel implements ActionListener{
private Image image;

public testClipboard() {
setPreferredSize(new Dimension(100, 100));
JButton jb = new JButton("Copy");
jb.addActionListener(this);
this.add(jb);
image = new BufferedImage(100, 100,
BufferedImage.TYPE_INT_RGB);
Graphics g = image.getGraphics();
g.setColor(Color.WHITE);
g.fillRect(0, 0, 99, 99);
g.setColor(Color.RED);
g.drawOval(10, 50, 20, 20);
}

@Override
public void actionPerformed(ActionEvent e) {
copyToClipboard();
}

public void copyToClipboard(){
Clipboard clipboard = Toolkit.getDefaultToolkit
().getSystemClipboard();
ImageTransferable selection = new ImageTransferable
(image);
clipboard.setContents(selection, null);
}

public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
@Override
public void run() {
JFrame f = new JFrame();
f.setDefaultCloseOperation
(JFrame.EXIT_ON_CLOSE);
f.add(new testClipboard
(),BorderLayout.CENTER);
f.pack();
f.setVisible(true);
}
});
}

class ImageTransferable implements Transferable{
public ImageTransferable(Image image){
theImage = image;
}
public DataFlavor[] getTransferDataFlavors(){
return new DataFlavor[]
{ DataFlavor.imageFlavor };
}
public boolean isDataFlavorSupported(DataFlavor flavor){
return flavor.equals(DataFlavor.imageFlavor);
}
public Object getTransferData(DataFlavor flavor) throws
UnsupportedFlavorException{
if (flavor.equals(DataFlavor.imageFlavor)){
return theImage;
}
else{
throw new UnsupportedFlavorException
(flavor);
}
}
private Image theImage;
}
}

rossum

unread,
Jun 2, 2011, 6:05:33 PM6/2/11
to
On 02 Jun 2011 19:35:01 GMT, ricoh51 <ric...@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'.

rossum


> }
>

ricoh51

unread,
Jun 2, 2011, 6:30:55 PM6/2/11
to

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

rossum

unread,
Jun 2, 2011, 7:28:35 PM6/2/11
to

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


John B. Matthews

unread,
Jun 3, 2011, 12:19:45 AM6/3/11
to
In article <4de7e5e5$0$7305$426a...@news.free.fr>,
ricoh51 <ric...@free.fr> wrote:

> I have problems to place an image on the clipboard (ubuntu 10.10).
> The code is below, and for example Gimp says that there is nothing in
> the clipboard!

I can verify that your example works on Mac OS X; the desktop has a
"Show Clipboard" command in the Edit menu. The same code appears to fail
on Ubuntu 10.04, but how does one verify? I can see that my image
viewer's paste command is disabled, but I'm not sure that's reliable.

As an aside, I must commend your use of a synthetic image. It makes the
example admirably self-contained.

--
John B. Matthews
trashgod at gmail dot com
<http://sites.google.com/site/drjohnbmatthews>

ricoh51

unread,
Jun 3, 2011, 5:15:54 AM6/3/11
to
On Fri, 03 Jun 2011 00:19:45 -0400, John B. Matthews wrote:

> In article <4de7e5e5$0$7305$426a...@news.free.fr>,
> ricoh51 <ric...@free.fr> wrote:
>
>> I have problems to place an image on the clipboard (ubuntu 10.10). The
>> code is below, and for example Gimp says that there is nothing in the
>> clipboard!
>
> I can verify that your example works on Mac OS X; the desktop has a
> "Show Clipboard" command in the Edit menu. The same code appears to fail
> on Ubuntu 10.04, but how does one verify? I can see that my image
> viewer's paste command is disabled, but I'm not sure that's reliable.

It appears that this is a (old) bug from Sun :
http://bugs.sun.com/bugdatabase/view_bug.do?bug_id=6607163
and certainly from OpenJDK too...

Incredible! No way to copy an image from a java program into the clipboard
(imageJ and MarvinSketch are unable to copy an image...)

Thank's

eric

John B. Matthews

unread,
Jun 3, 2011, 3:01:31 PM6/3/11
to
In article <4de8a64a$0$25000$426a...@news.free.fr>,
ricoh51 <ric...@free.fr> wrote:

Thank you for the bug link. Perhaps ImageIO.write() could offer a
work-around, albeit less convenient.

<http://download.oracle.com/javase/6/docs/api/javax/imageio/ImageIO.html>

蠅量情書

unread,
Jun 6, 2011, 7:02:47 PM6/6/11
to

ronal...@gmail.com wrote:

> On 12 feb, 12:06, Nigel Wade <n...@ion.le.ac.uk> wrote:
>> Ronald Rood wrote:
>> > No matter which one I try, both end up with the same problem, when I try
java
>> > -version I get 'lang/java/Object' missing.
>>
>> Are you sure you get this error with the self-extracting file?
> Yes.
>
> Thanks for the reply Nigel,
> maybe what I did was/is wrong, I don't know. The self-extracting file
> first unpacked the rpm file and then tried to install the rpm. I have
> no idea which files I should have to be complete. I noticed I do have
> rt.pack and no rt.jar. This could be solved by unpack rt.pack rt.jar
> and after that java -version works as supposed to.
> So my conclusion is that my action could possibly be done better but
> was not that bad after all.
>
> Did you also try with the jre ? you downloaded the jdk.

--
[;32m※ Origin: 楓橋驛站<bbs.cs.nthu.edu.tw>
[;32m◆ From: totentanz @ 111-255-7-210.dynamic.hinet.net [m

0 new messages