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

copying image to system clipboard

5 views
Skip to first unread message

Matthew Fleming

unread,
Aug 13, 2002, 2:34:30 PM8/13/02
to
Javameisters,

I am trying to copy an image to the system clipboard. In principle this
should be straightforward with Java 1.4. The problem I'm having is when I
copy two large images, one after the other. The first image is copied to the
clipboard, but the second one is not; the operation fails with:

java.lang.IllegalStateException: cannot open system clipboard
at sun.awt.windows.WClipboard.openClipboard(Native Method)
at sun.awt.windows.WClipboard.setContents(WClipboard.java:65)
at ImStore.ImageTransfer.<init>(ImageTransfer.java:24)

This problem does not occur with small images, I can copy as many of them as
I want.
I have included below the code for the small class that does the copying
(this class is used by creating an instance in an action listener in a
JFrame; the constructor for the class accepts a BufferedImage reference from
the JFrame).

Any suggestions much appreciated. I really have no idea what is wrong here.

Regards,

Matthew Fleming

/*
* ImageTransfer.java
*
* Created on August 13, 2002, 12:43 PM
*/

package ImStore;
import java.awt.datatransfer.*;
import java.awt.image.*;
import java.awt.*;
import javax.swing.*;

/**
*
* @author Matthew Fleming
*/
public class ImageTransfer implements Transferable, ClipboardOwner {
BufferedImage bi;
/** Creates a new instance of ImageTransfer */
public ImageTransfer(BufferedImage bi) {
this.bi=bi;
Clipboard
clipboard=Toolkit.getDefaultToolkit().getSystemClipboard();
System.gc();
clipboard.setContents(this,null);
}

public Object getTransferData(java.awt.datatransfer.DataFlavor
dataFlavor) throws java.awt.datatransfer.UnsupportedFlavorException,
java.io.IOException {
return bi;
}

public java.awt.datatransfer.DataFlavor[] getTransferDataFlavors() {
return new DataFlavor[] { DataFlavor.imageFlavor };
}

public boolean isDataFlavorSupported(java.awt.datatransfer.DataFlavor
dataFlavor) {
return DataFlavor.imageFlavor.equals(dataFlavor);
}

public void lostOwnership(java.awt.datatransfer.Clipboard clipboard,
java.awt.datatransfer.Transferable transferable) {
if (bi!=null) bi.flush();
bi=null;
}

0 new messages