# Dateien über Clipboard kopieren



## wurtzel (28. Feb 2006)

Hallo

ich bin gerade an einem Projekt bei dem ich Dateien (Bilder .wmf, .gif, .png) in das Clipboard bringen muß damit sie von anderen Anwendungen eingebunden werden können (z.B Powerpoint)
dies sollte von der Kommandozeile aus funktionieren.

die folgende variante funktioniert leider nur bei Powerpoint 2003 und openoffice
wo liegt das problem das es nicht bei älteren versionen funktioniert


```
public void copytoClipboard(String content)
    {

// content ist der Dateiname
        Vector fileVector = new Vector();
        File f = new File(new FileName(content).toString());
        if (debug) System.out.println("3" + f.getPath());
        f = f.getAbsoluteFile();
        if (debug) System.out.println("4" + f.getPath());
        fileVector.add(f.toString());
        int wert = fileVector.size();
        if (debug) System.out.println("5 " + wert);
        JList dragFileLister = new JList(fileVector);
        // dragFileLister.setDragEnabled(true);

        dragFileLister.setTransferHandler(new ListTransferHandler(fileVector));
        final Clipboard clipboard = Toolkit.getDefaultToolkit().getSystemClipboard();
        TransferHandler handler = dragFileLister.getTransferHandler();
        handler.exportToClipboard(dragFileLister, clipboard, TransferHandler.COPY);

    }
```
Class ListTransferHandler

```
import java.awt.datatransfer.Clipboard;
import java.awt.datatransfer.Transferable;

import java.util.Vector;

import javax.swing.JComponent;
import javax.swing.JList;
import javax.swing.TransferHandler;

public class ListTransferHandler extends TransferHandler
{
   private String dir;
   private Vector files ;

   public ListTransferHandler()
   {
      files = new Vector();
   }

   public ListTransferHandler(Vector v)
   {
      files = v;
   }

 
   public ListTransferHandler(String dir ,Vector v)
   {
      if( dir.endsWith("/") )
         this.dir = dir;
      else
         this.dir = dir+"/";
      files = v;
   }

   public ListTransferHandler(String dir)
   {
      if( dir.endsWith("/") )
         this.dir = dir;
      else
         this.dir = dir+"/";
      files = new Vector();
   }


   public int getSourceActions(JComponent c)
   {
      System.out.println("getSourceActions");
      return COPY;  // or return COPY_OR_MOVE;
   }

   public Transferable createTransferable(JComponent c)
   {
      JList list = (JList) c;  // we know it's a JList
      //Object[] values = list.getSelectedValues();  // strings
      Vector files = new Vector();
     
      String listEntry ;
     if(!this.files.isEmpty())files=this.files;
      TransferableFile  tf = new TransferableFile(files);
      return tf;
   }

  

   public void exportToClipboard(JComponent comp, Clipboard clip, int action)
      throws IllegalStateException
   {
      System.out.println("exportToClipboard");
      super.exportToClipboard(comp,clip,action);
   }

   // Causes the Swing drag support to be initiated.
   public void exportAsDrag(JComponent comp, java.awt.event.InputEvent e, int action)
   {
      System.out.println("exportAsDrag");
      super.exportAsDrag(comp, e, action);
   }

   //Invoked after data has been exported.
   public void exportDone(JComponent source, Transferable data, int action)
   {
      System.out.println("exportDone");
      super.exportDone(source, data, action) ;
   }
  

} // end class ListTransferHandler extends TransferHandler
```
TransferableFile

```
import java.io.*;
import java.util.*;
import java.awt.datatransfer.*;

public class TransferableFile implements Transferable
{
   private Vector fileList;

   public TransferableFile(String fileName)
   {
      fileList = new Vector();
      fileList.add( new File(fileName) ) ;
   }

   public TransferableFile(File file)
   {
      fileList = new Vector();
      fileList.add( file ) ;
   }

   public TransferableFile(String[] fileNames)
   {
      fileList = new Vector();
      for(int i=0; i<fileNames.length; i++)
         fileList.add( new File(fileNames[i]) ) ;
   }

   public TransferableFile(File[] files)
   {
      fileList = new Vector();
      for(int i=0; i<files.length; i++)
         fileList.add( files[i] ) ;
   }

   public TransferableFile(Vector files)
   {
      // we had to make a copy, because the elements of files can change !
      fileList = new Vector(files);
   }

   //Returns an object which represents the data to be transferred.
   public Object getTransferData(DataFlavor flavor)
      throws UnsupportedFlavorException
   {
      if( flavor.equals(DataFlavor.javaFileListFlavor) )
         return fileList ;

      throw new UnsupportedFlavorException(flavor);
   }

   //Returns an array of DataFlavor objects indicating the flavors
   //the data can be provided in.
   public DataFlavor[] getTransferDataFlavors()
   {
      return new DataFlavor[] {DataFlavor.javaFileListFlavor} ;
   }

   //Returns whether or not the specified data flavor is supported for this object.
   public boolean isDataFlavorSupported(DataFlavor flavor)
   {
      return flavor.equals(DataFlavor.javaFileListFlavor) ;
   }
}
```


----------



## MPW (1. Mrz 2006)

Hm, du koenntest dir mal angucken, wie das in den aelteren Versionen implementier ist....in dem du von da aus was in die zwischenablage legst und dann wieder mit Java ausliest, in dem gleichen Format musst du das auch wieder reinstecken.


----------



## wurtzel (1. Mrz 2006)

Die vom Explorer erzeugten ZA Formate
Shell IDList Array
CF_HDROP
DataObjectAttributes
Preferred DropEffect
Shell Object Offsets
FileName
FileNameW

Die vom JAVA erzeugten ZA Formate
Preferred DropEffect
CF_HDROP

Mein Problem ist wie erzeuge ich die anderen


----------

