How can I copy data from Clipboard to string ??

Recommended Answers

All 4 Replies

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();
		}
	}
}

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

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.