Skip to main content

[bug] Copy to clipboard mac os x mountain lion and java 7

4 replies [Last post]
mazugrazus
Offline
Joined: 2009-01-02
Points: 0

I have problem, that i cannot copy image in mac under java 7
code sample:
public void copyToClipboard(@Nonnull ProjectsMapComponent mapComponent){
MapDisplay activeProjectUsageMapDisplay = mapComponent.getActiveView()
Dimension offSize = activeProjectUsageMapDisplay.getOffSize();
PrefuseScrollPane activeComponentScroll = mapComponent.getActiveComponentScroll();
int height = activeComponentScroll.getHorizontalScrollBar().getHeight();
int width = activeComponentScroll.getVerticalScrollBar().getWidth();
final BufferedImage bufferedImage = new BufferedImage( ((int)offSize.getWidth() - height - IMAGE_SIZE_MISTAKE_CORRECTOR),
   ((int)offSize.getHeight() - width - IMAGE_SIZE_MISTAKE_CORRECTOR),
   BufferedImage.TYPE_INT_ARGB);
Graphics graphics = bufferedImage.getGraphics();
paintBufferedImage(graphics, activeProjectUsageMapDisplay, activeComponentScroll);
graphics.dispose();
Transferable transferableImage = getTransferableImage(bufferedImage);
Toolkit.getDefaultToolkit().getSystemClipboard().setContents(transferableImage, null);
}

------------------

public void paintBufferedImage(Graphics g, MapDisplay mapDisplay, PrefuseScrollPane activeComponentScroll)
{
JScrollBar hScrollBar = activeComponentScroll.getHorizontalScrollBar();
JScrollBar vScrollBar = activeComponentScroll.getVerticalScrollBar();
BoundedRangeModel hModel = hScrollBar.getModel();
BoundedRangeModel vModel = vScrollBar.getModel();
int oldH = hModel.getValue();
int oldV = vModel.getValue();
//we should slide scrollBars at the beginning to have start coordinates at (0,0)
hModel.setValue(hModel.getValue() > 0 ? 0 : hModel.getValue());
vModel.setValue(vModel.getValue() > 0 ? 0 : vModel.getValue());
mapDisplay.setDamageRedraw(true);
mapDisplay.printDisplay(g);
hModel.setValue(oldH);
vModel.setValue(oldV);
}

------------------

private Transferable getTransferableImage(final BufferedImage bufferedImage){
return new Transferable()
{
@Override
public DataFlavor[] getTransferDataFlavors()
{
return new DataFlavor[]{DataFlavor.imageFlavor};
}
@Override
public boolean isDataFlavorSupported(DataFlavor flavor)
{
return DataFlavor.imageFlavor.equals(flavor);
}
public Object getTransferData(DataFlavor flavor) throws UnsupportedFlavorException, IOException
{
if(DataFlavor.imageFlavor.equals(flavor))
{
return bufferedImage;
}
return null;
}
};
}

Under java 6 it works fine, under java 7 and windows it works fine too, but under mac it do not work.
I always get:

java.io.IOException: Data translation failed: not an image format
at sun.awt.datatransfer.DataTransferer.translateTransferable(DataTransferer.java:1293)
at sun.lwawt.macosx.CDataTransferer.translateTransferable(CDataTransferer.java:131)
at sun.lwawt.macosx.CClipboard.setContentsNative(CClipboard.java:76)
at sun.awt.datatransfer.SunClipboard.setContents(SunClipboard.java:110)

any way to fix it? a work around? or a hack? would be very helpful.

Reply viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.
hars.gupta
Offline
Joined: 2012-10-25
Points: 0

mazugrazus wrote:
I have problem, that i cannot copy image in mac under java 7
code sample:
public void copyToClipboard(@Nonnull ProjectsMapComponent mapComponent){
MapDisplay activeProjectUsageMapDisplay = mapComponent.getActiveView()
Dimension offSize = activeProjectUsageMapDisplay.getOffSize();
PrefuseScrollPane activeComponentScroll = mapComponent.getActiveComponentScroll();
int height = activeComponentScroll.getHorizontalScrollBar().getHeight();
int width = activeComponentScroll.getVerticalScrollBar().getWidth();
final BufferedImage bufferedImage = new BufferedImage( ((int)offSize.getWidth() - height - IMAGE_SIZE_MISTAKE_CORRECTOR),
   ((int)offSize.getHeight() - width - IMAGE_SIZE_MISTAKE_CORRECTOR),
   BufferedImage.TYPE_INT_ARGB);
Graphics graphics = bufferedImage.getGraphics();
paintBufferedImage(graphics, activeProjectUsageMapDisplay, activeComponentScroll);
graphics.dispose();
Transferable transferableImage = getTransferableImage(bufferedImage);
Toolkit.getDefaultToolkit().getSystemClipboard().setContents(transferableImage, null);
}

------------------

public void paintBufferedImage(Graphics g, MapDisplay mapDisplay, PrefuseScrollPane activeComponentScroll)
{
JScrollBar hScrollBar = activeComponentScroll.getHorizontalScrollBar();
JScrollBar vScrollBar = activeComponentScroll.getVerticalScrollBar();
BoundedRangeModel hModel = hScrollBar.getModel();
BoundedRangeModel vModel = vScrollBar.getModel();
int oldH = hModel.getValue();
int oldV = vModel.getValue();
//we should slide scrollBars at the beginning to have start coordinates at (0,0)
hModel.setValue(hModel.getValue() > 0 ? 0 : hModel.getValue());
vModel.setValue(vModel.getValue() > 0 ? 0 : vModel.getValue());
mapDisplay.setDamageRedraw(true);
mapDisplay.printDisplay(g);
hModel.setValue(oldH);
vModel.setValue(oldV);
}

------------------

private Transferable getTransferableImage(final BufferedImage bufferedImage){
return new Transferable()
{
@Override
public DataFlavor[] getTransferDataFlavors()
{
return new DataFlavor[]{DataFlavor.imageFlavor};
}
@Override
public boolean isDataFlavorSupported(DataFlavor flavor)
{
return DataFlavor.imageFlavor.equals(flavor);
}
public Object getTransferData(DataFlavor flavor) throws UnsupportedFlavorException, IOException
{
if(DataFlavor.imageFlavor.equals(flavor))
{
return bufferedImage;
}
return null;
}
};
}

Under java 6 it works fine, under java 7 and windows it works fine too, but under mac it do not work.
I always get:

java.io.IOException: Data translation failed: not an image format
at sun.awt.datatransfer.DataTransferer.translateTransferable(DataTransferer.java:1293)
at sun.lwawt.macosx.CDataTransferer.translateTransferable(CDataTransferer.java:131)
at sun.lwawt.macosx.CClipboard.setContentsNative(CClipboard.java:76)
at sun.awt.datatransfer.SunClipboard.setContents(SunClipboard.java:110)

any way to fix it? a work around? or a hack? would be very helpful.

Just to add into the above problem. I am also facing the same issue when I tried copying a file or folder programatically, from system itself to clipboard

Code :

public void setClipBoardData(String path) {
Clipboard clipboard = Toolkit.getDefaultToolkit().getSystemClipboard();
List copiedFiles = new ArrayList();
copiedFiles.add(new File(path));
Transferable transferable = new FileListTransferable(copiedFiles);
clipboard.setContents(transferable, clipboardOwner);
}

public class FileListTransferable implements Transferable {
List fileList; // The list of files

public FileListTransferable(List files) {
fileList = new ArrayList();
for (int i = 0; i < files.size(); i++) {

fileList.add(files.get(i));

}
}

// Implementation of the Transferable interface
public DataFlavor[] getTransferDataFlavors() {
return new DataFlavor[] { DataFlavor.javaFileListFlavor };
}

public boolean isDataFlavorSupported(DataFlavor fl) {
return fl.equals(DataFlavor.javaFileListFlavor);
}

public Object getTransferData(DataFlavor fl) {
if (!isDataFlavorSupported(fl)) {
return null;
}

return fileList;
}
}

Works fine in windows but not in MAC system(No exception is occurring, clipboard is blank)... Please help

kovzol
Offline
Joined: 2013-10-19
Points: 0

Have you finally filed a bug report ? It seems this problem is still there in Oracle Java 1.7.40.

hars.gupta
Offline
Joined: 2012-10-25
Points: 0

No, I din't filed a bug report. :(

anthony_p
Offline
Joined: 2006-07-24
Points: 0

Please file a bug at http://bugs.sun.com/