Hi Terry,
here is a response from one of guys from Drag'N Drop team:
// DnD responds:
> In the java program I am developing, I can copy an image
> created by this program into the clipboard and paste it into
> another image also created by this program. However, if I
> open the clipboard viewer program to see what is inside the
> system clipboard, the image is not there. Why? Does Java
> bypass the system clipboard?
>
This depends on the data flavor used for image transfer in your
applications.
Java applications operate with platform-independent data flavors.
Native applications (including the clipboard viewer) operate with
platform-specific data formats. Data transfer between Java and
native applications can be performed only if there is a supported
mapping between the data flavors used by the Java application and
the data formats used by the native application.
The mappings supported by the Java Data Transfer subsystem are listed
in lib\flavormap.properties file of your JRE installation.
Prior to J2SE 1.4, there were no supported mappings for image
transfer, so image transfer between applications was unsupported.
It was possible to transfer images only within a Java application.
Image transfer support was expanded in J2SE 1.4. The new capabilities
enable image transfer within and between Java applications and between
Java and native applications. Your application should use
DataFlavor.imageFlavor to export and import image data.
> If I open an image in a non-java program like Photoshop
> and copy the image into clipboard, is there a way to have
> my java program to read the clipboard content and fetch
> the image data in the clipboard?
With J2SE 1.4, use Clipboard.getContents() to get the Transferable
representing the clipboard contents and then retrieve image data
using DataFlavor.imageFlavor.
Attached is a test case that demonstrates image transfer via the
system clipboard. I'm also attaching a sample image for convenience.
I verified that an image copied to the system clipboard by this test
case is properly displayed in the clipboard viewer.
// DnD responds
Please see the test case in the attachment.
Thank you,
Dmitri
On Mon, Oct 01, 2001 at 08:55:0p -0700, Terry Wu wrote:
> Hi,
>
> In the java program I am developing, I can copy an image
> created by this program into the clipboard and paste it into
> another image also created by this program. However, if I
> open the clipboard viewer program to see what is inside the
> system clipboard, the image is not there. Why? Does Java
> bypass the system clipboard?
>
> If I open an image in a non-java program like Photoshop
> and copy the image into clipboard, is there a way to have
> my java program to read the clipboard content and fetch
> the image data in the clipboard?
>
> Thanks in advance for your help,
>
> Terry
>
> __________________________________________________
> Do You Yahoo!?
> Listen to your Yahoo! Mail messages from any phone.
> http://phone.yahoo.com
>
> ===========================================================================
> To unsubscribe, send email to [EMAIL PROTECTED] and include in the body
> of the message "signoff JAVA2D-INTEREST". For general help, send email to
> [EMAIL PROTECTED] and include in the body of the message "help".
import java.awt.*;
import java.awt.event.*;
import java.awt.datatransfer.*;
import javax.swing.*;
public class ClipboardTest implements ActionListener, Transferable {
final Toolkit toolkit = Toolkit.getDefaultToolkit();
final Clipboard clipboard = toolkit.getSystemClipboard();
Image image = null;
final JFrame frame = new JFrame();
final JLabel label = new JLabel();
final JButton button1 = new JButton("Copy");
final JButton button2 = new JButton("Paste");
final JPanel pastePanel = new JPanel();
public static void main(String[] args) {
if (args.length == 0) {
System.out.println("No image loaded. To load an image:");
System.out.println(" java ClipboardTest <image file>");
}
ClipboardTest test = new ClipboardTest(args.length > 0 ? args[0] : null);
}
public ClipboardTest(String imageName) {
if (imageName != null) {
image = toolkit.createImage(imageName);
label.setIcon(new ImageIcon(image));
}
button1.addActionListener(this);
button2.addActionListener(this);
frame.getContentPane().setLayout(new FlowLayout());
frame.getContentPane().add(label);
frame.getContentPane().add(button1);
frame.getContentPane().add(button2);
frame.getContentPane().add(pastePanel);
frame.pack();
frame.setVisible(true);
}
public void actionPerformed(ActionEvent e) {
try {
if (e.getSource() == button1) {
clipboard.setContents(this, null);
} else {
final Transferable transferable = clipboard.getContents(null);
if (transferable.isDataFlavorSupported(DataFlavor.imageFlavor)) {
final Image img =
(Image)transferable.getTransferData(DataFlavor.imageFlavor);
pastePanel.add(new JLabel(new ImageIcon(img)));
} else {
System.err.println("No image data in the system clipboard.");
}
frame.pack();
frame.invalidate();
frame.validate();
frame.repaint();
}
} catch (Throwable t) {
t.printStackTrace();
}
}
public DataFlavor[] getTransferDataFlavors() {
return new DataFlavor[] { DataFlavor.imageFlavor };
}
public boolean isDataFlavorSupported(DataFlavor flavor) {
return DataFlavor.imageFlavor.equals(flavor);
}
public Object getTransferData(DataFlavor flavor)
throws UnsupportedFlavorException {
if (!isDataFlavorSupported(flavor)) {
throw new UnsupportedFlavorException(flavor);
}
return image;
}
}
b.gif