Getting and Setting Text on the System Clipboard

This examples defines methods for getting and setting text on the system clipboard.
// If a string is on the system clipboard, this method returns it; // otherwise it returns null. public static String getClipboard() { Transferable t = Toolkit.getDefaultToolkit().getSystemClipboard().getContents(null); try { if (t != null && t.isDataFlavorSupported(DataFlavor.stringFlavor)) { String text = (String)t.getTransferData(DataFlavor.stringFlavor); return text; } } catch (UnsupportedFlavorException e) { } catch (IOException e) { } return null; } // This method writes a string to the system clipboard. // otherwise it returns null. public static void setClipboard(String str) { StringSelection ss = new StringSelection(str); Toolkit.getDefaultToolkit().getSystemClipboard().setContents(ss, null); }

Comments

4 Feb 2010 - 10:00pm by Sebastian (not verified)

Awesome help bro! thnx

Post a comment

More information about formatting options

CAPTCHA
This question is for testing whether you are a human visitor and to prevent automated spam submissions.
Image CAPTCHA
Enter the characters shown in the image. Ignore spaces and be careful about upper and lower case.