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;
}
}
> 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
> }
>
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
> 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>
> 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
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>
> 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