How can I copy data from Clipboard to string ??

following example will take whatever text in your clipboard into string and will print on console

import java.awt.HeadlessException;
import java.awt.Toolkit;
import java.awt.datatransfer.DataFlavor;
import java.awt.datatransfer.UnsupportedFlavorException;
import java.io.IOException;

public class ClipboardCopyMaker {

	public static void main(String[] args) {

		copyToSystemClipboard();
	}

	public static void copyToSystemClipboard() {
		try {
			String dataFlavor = Toolkit.getDefaultToolkit()
					.getSystemClipboard().getData(DataFlavor.stringFlavor)
					.toString();
			System.out.println(dataFlavor);
		} catch (HeadlessException e) {
			e.printStackTrace();
		} catch (UnsupportedFlavorException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
}

Edited 7 Years Ago by javed123: n/a

Interesting, Javed123.
This appears to be version of the code in the link I posted earlier, but with less checking, and totally misleading names ("copyToSystemClipboard" copies from the clipboard, "dataFlavor" holds the data String, not the DataFlavor).
Not sure what you are trying to contribute here...

Interesting, Javed123.
This appears to be version of the code in the link I posted earlier, but with less checking, and totally misleading names ("copyToSystemClipboard" copies from the clipboard, "dataFlavor" holds the data String, not the DataFlavor).
Not sure what you are trying to contribute here...

@JamesCherrill

Yes, there are misleading names in sample code i had posted and this happened because I was trying both programs at same time
1. copying from system clipboard
2. copying to system clipboard

I think I am giving here what thread poster is looking for.

:-O:-O

This article has been dead for over six months. Start a new discussion instead.