che io sappia, con le immagini non si può fare senza aggiungere del codice opportuno per trasportare i dati nella clipboard. Con i testi in text components invece è sempre possibile copiare via selezione e CTRL+C. Questo è un esempio completo, con codice rimaneggiato dalla guida della sun, su come copiare un'immagine nella clipboard
codice:
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.awt.datatransfer.*;
import java.io.*;
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]);
}
}
public class displayImages extends JFrame implements ActionListener {
File f;
JLabel img;
Image image;
JScrollPane sc;
JButton open,copy;
private ImageIcon createImageIcon(String path) {
try {
return new ImageIcon(path);
}
catch (Exception e) {
System.out.println(e.toString());
return null;
}
}
public void actionPerformed (ActionEvent ae) {
if (((JButton)(ae.getSource())).equals(open)) {
try {
JFileChooser fc = new JFileChooser();
fc.showDialog(this, "Choose Image");
f = fc.getSelectedFile();
try {
img.setVisible(false);
sc.setVisible(false);
}
catch (Exception ex) {}
img = new JLabel(createImageIcon(f.getAbsolutePath()));
img.setTransferHandler(new ImageSelection());
image = createImageIcon(f.getAbsolutePath()).getImage();
sc = new JScrollPane(img);
this.getContentPane().add(sc, BorderLayout.CENTER);
this.validate();
}
catch (Exception e) {}
}
else { //è stato premuto copy to clipboard
if (!image.equals(null)) {
Toolkit kit = Toolkit.getDefaultToolkit();
Clipboard clipboard = kit.getSystemClipboard();
TransferHandler handler = img.getTransferHandler();
// Copy
handler.exportToClipboard(img, clipboard, TransferHandler.COPY);
}
}
}
public displayImages() {
super("Image display");
open = new JButton("Open...");
copy = new JButton("Copy to Clipboard");
this.getContentPane().setLayout(new BorderLayout());
this.getContentPane().add(open, BorderLayout.NORTH);
this.getContentPane().add(copy, BorderLayout.SOUTH);
open.addActionListener(this);
copy.addActionListener(this);
this.setSize(300, 300);
this.setVisible(true);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
public static void main (String[] args) {
displayImages d = new displayImages();
}
}
1