participate


Abstract Window Toolkit (AWT) - how to paste image through system clipboard
<<   Back to Forum  |   Give us Feedback
This topic has 7 replies on 1 page.
zhj2610
Posts:1
Registered: 9/24/00
how to paste image through system clipboard   
Oct 28, 2001 8:46 PM

 
hi,

I must copy a image from java application to another graphic application such as photoshot. I think I must paste the image to system clipboard.
I can copy/paste a image by a clipboard made by myself(e.g. Clicpboard clipboard = new Clipboard("MyClicpboard"))). but I have no idea how to do it with system clipboard. below is the exception i can not get rid of:

java.lang.ClassCastException: [I
at sun.awt.windows.WDataTransferer.translateTransferable(WDataTransferer.java:390)
at sun.awt.DataTransferer.translateTransferable(DataTransferer.java:245)
at sun.awt.windows.WClipboard.setContents(WClipboard.java:69)
at Test.<init>(Test.java:40)
at Test.main(Test.java:12)

please give me some advise.
thanks in advance!
 
swingfreak
Posts:569
Registered: 8/8/01
Re: how to paste image through system clipboard   
Oct 29, 2001 4:47 AM (reply 1 of 7)  (In reply to original post )

 
hello

try this:

Clipboard myClipboard = Toolkit.getDefaultToolkit().getSystemClipboard();


may this works
You have to import the awt to use toolkit.

regards
 
alanmarcinkowski
Posts:4
Registered: 8/17/01
Re: how to paste image through system clipboard   
Dec 5, 2001 5:11 AM (reply 2 of 7)  (In reply to #1 )

 
I'm seeing this exception now as well in code that used to work in previous flavors of 1.3. Any ideas? The object appears to get copied but the code always throws on the setContents call. Anyone in the AWT group look at this?
 
alanmarcinkowski
Posts:4
Registered: 8/17/01
Re: how to paste image through system clipboard   
Dec 5, 2001 5:12 AM (reply 3 of 7)  (In reply to #2 )

 
...And, yes, I am seeing it using the:

Clipboard clip = Toolkit.getDefaultToolkit().getSystemClipboard();

Then call setContents (Transferable t, ClipboardOwner o) where t is not null and o is (but it didn't seem to matter even when I provided and owner, it still threw).
 
asskikin
Posts:1
Registered: 8/27/99
Re: how to paste image through system clipboard   
Apr 11, 2002 9:27 AM (reply 4 of 7)  (In reply to #3 )

 
Probably to late a response for this guy but it would appear that the problem he is having
is in his DataFlavor construction :

DataFlavor(Class representationClass, String humanPresentableName);


Ex.
public static final DataFlavor flavorDiagramFragment = new DataFlavor(MyClass.class, "desc.");

If the representationClass parameter does not correctly reflect the transferable classes data you will get the ClassCastException.

Doug
 
aragon28
Posts:82
Registered: 2/5/03
Re: how to paste image through system clipboard   
Feb 20, 2003 7:31 PM (reply 5 of 7)  (In reply to #4 )

 
check this 2 java files

import java.awt.BorderLayout;
import java.awt.Container;
import java.awt.Toolkit;
import java.awt.datatransfer.Clipboard;
import java.awt.datatransfer.DataFlavor;
import java.awt.datatransfer.Transferable;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.Icon;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.TransferHandler;

public class ImageCopy {

public static void main(String args[]) {

JFrame frame = new JFrame("Copy Image");
frame.setDefaultCloseOperation
(JFrame.EXIT_ON_CLOSE);

Container contentPane = frame.getContentPane();

Toolkit kit = Toolkit.getDefaultToolkit();
final Clipboard clipboard =
kit.getSystemClipboard();

Icon icon = new ImageIcon("scott.gif");
final JLabel label = new JLabel(icon);
label.setTransferHandler(new ImageSelection());

JScrollPane pane = new JScrollPane(label);
contentPane.add(pane, BorderLayout.CENTER);

JButton copy = new JButton("Label Copy");
copy.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
TransferHandler handler =
label.getTransferHandler();
handler.exportToClipboard(label, clipboard,
TransferHandler.COPY);
}
});

JButton clear = new JButton("Label Clear");
clear.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent
actionEvent) {
label.setIcon(null);
}
});

JButton paste = new JButton("Label Paste");
paste.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent
actionEvent) {
Transferable clipData =
clipboard.getContents(clipboard);
if (clipData != null) {
if (clipData.isDataFlavorSupported
(DataFlavor.imageFlavor)) {
TransferHandler handler =
label.getTransferHandler();
handler.importData(label, clipData);
}
}
}
});

JPanel p = new JPanel();
p.add(copy);
p.add(clear);
p.add(paste);
contentPane.add(p, BorderLayout.NORTH);

JPanel pasteP = new JPanel();
JButton pasteB = new JButton("Paste");

pasteB.setTransferHandler(new ImageSelection());

pasteB.addActionListener
(TransferHandler.getPasteAction());

pasteP.add(pasteB);
contentPane.add(pasteB, BorderLayout.SOUTH);

frame.setSize(400, 400);
frame.show();
}
}


/
@author daniel

To change this generated comment edit the template variable "typecomment":
Window>Preferences>Java>Templates.
To enable and disable the creation of type comments go to
Window>Preferences>Java>Code Generation.
*/
import java.awt.Image;
import java.awt.datatransfer.DataFlavor;
import java.awt.datatransfer.Transferable;
import java.awt.datatransfer.UnsupportedFlavorException;
import java.io.IOException;

import javax.swing.AbstractButton;
import javax.swing.Icon;
import javax.swing.ImageIcon;
import javax.swing.JComponent;
import javax.swing.JLabel;
import javax.swing.TransferHandler;

public class ImageSelection extends TransferHandler
implements Transferable {

private static final DataFlavor flavors[] =
{DataFlavor.imageFlavor};

private Image image;

public int getSourceActions(JComponent c) {
return TransferHandler.COPY;
}

public boolean canImport(JComponent comp, DataFlavor
flavor[]) {
if (!(comp instanceof JLabel) ||
(comp instanceof AbstractButton)) {
return false;
}
for (int i=0, n=flavor.length; i<n; i++) {
if (flavor[i].equals(flavors[0])) {
return true;
}
}
return false;
}

public Transferable createTransferable(JComponent
comp) {
// Clear
image = null;
Icon icon = null;

if (comp instanceof JLabel) {
JLabel label = (JLabel)comp;
icon = label.getIcon();
} else if (comp instanceof AbstractButton) {
AbstractButton button = (AbstractButton)comp;
icon = button.getIcon();
}
if (icon instanceof ImageIcon) {
image = ((ImageIcon)icon).getImage();
return this;
}
return null;
}

public boolean importData(JComponent comp,
Transferable t) {
ImageIcon icon = null;
try {
if (t.isDataFlavorSupported(flavors[0])) {
image = (Image)t.getTransferData(flavors[0]);
icon = new ImageIcon(image);
}
if (comp instanceof JLabel) {
JLabel label = (JLabel)comp;
label.setIcon(icon);
return true;
} else if (comp instanceof AbstractButton) {
AbstractButton button = (AbstractButton)comp;
button.setIcon(icon);
return true;
}

} catch (UnsupportedFlavorException ignored) {
} catch (IOException ignored) {
}
return false;
}

// Transferable
public Object getTransferData(DataFlavor flavor) {
if (isDataFlavorSupported(flavor)) {
return image;
}
return null;
}

public DataFlavor[] getTransferDataFlavors() {
return flavors;
}

public boolean isDataFlavorSupported(DataFlavor
flavor) {
return flavor.equals(flavors[0]);
}
}
 
jag_sun
Posts:14
Registered: 10/16/03
Re: how to paste image through system clipboard   
Oct 19, 2003 11:58 PM (reply 6 of 7)  (In reply to original post )

 
I too get a similar error now. Were U able to solve it. If so, plz tell me how?
 
Styraxian
Posts:1
Registered: 1/5/04
Re: how to paste image through system clipboard   
Jan 5, 2004 2:45 PM (reply 7 of 7)  (In reply to #6 )

 
I had the same problem but was able to fix it when I fixed my custom transferable to report the correct data flavor. See the post before the one with the code for a litte more information.
 
This topic has 7 replies on 1 page.
Back to Forum
 
Read the Developer Forums Code of Conduct

Click to email this message Email this Topic

Edit this Topic
  
 
 
Forums Statistics

About Sun forums
  • Oracle Forums is a large collection of user generated discussions. It is here to help you ask questions, find answers, and participate in discussions.

    Check out our guide on Getting started with Oracle Forums for a full walkthrough of how to best leverage the benefits of this community.

Powered by Jive Forums