SWT – Putting a file into the clipboard

I recently got the question how you can put a file into the system clipboard.

SWT makes this trivial:

[sourcecode language=”java”]

package de.vogella.desktop.clipboard;

import org.eclipse.swt.dnd.Clipboard;
import org.eclipse.swt.dnd.FileTransfer;
import org.eclipse.swt.dnd.Transfer;
import org.eclipse.swt.widgets.Display;

* Utility class for putting files into the system clipboard
* @author Lars Vogel

public final class CopyFileToClipboard {

private CopyFileToClipboard() {
// Utility class, prevent instantiation

* Copy a file into the clipboard
* Assumes the file exists -> no additional check
* @param fileName – includes the path

public static void copytoClipboard(String fileName) {
Display display = Display.getCurrent();
Clipboard clipboard = new Clipboard(display);
String[] data = { fileName };
clipboard.setContents(new Object[] { data },
new Transfer[] { FileTransfer.getInstance() });


Afer calling the method with a correct filename you should be able to paste the file, e.g. Strg+V into your system file browser.

About Lars Vogel

Lars Vogel is the founder and CEO of the vogella GmbH and works as Eclipse and Android consultant, trainer and book author. He is a regular speaker at international conferences, He is the primary author of vogella.com. With more than one million visitors per month this website is one of the central sources for Java, Eclipse and Android programming information.
This entry was posted in Eclipse and tagged . Bookmark the permalink.

5 Responses to SWT – Putting a file into the clipboard

Comments are closed.