In our project I am developing a simple function, which shall possible copying data to clipboard and later using it anywhere else.
It’s rather simple. A modal window pops up and shows in a SWT.List a list of all files exported by the function. This window has two more buttons: “OK” & “Copy To Clipboard”. OK Button closes windows by means of shell.dispose() (i have even tried Shell.close()).
Clipboard functionality looks this like:
Clipboard clipboard = new Clipboard(Display.getDefault());
TextTransfer transfer = TextTransfer.getInstance();
String lines = "";
for (String line : list.getItems())
{
lines += line;
lines +="\n";
}
clipboard.setContents(new Object[] { lines }, new TextTransfer[] { transfer });
When file export is finished, buttons are activated. Copying to Clipboard and using data is working, but only if I click this button and first paste this data anywhere. If I now click “OK” and close window, the data is still in clipboard.
But if I click “Copy” button and then directly “OK” without pasting this data anywhere, it’s lost from clipboard.
Is there any way to keep data in clipboard anyway?
Maybe you need to dispose of the clipboard, directly after setting the contents? That is, when you don’t need to use the Clipboard instance anymore, dispose it, as is documented in the Clipboard class javadoc. Not sure it helps, but you could try… See also http://www.vogella.com/blog/2009/09/04/swt-clipboard/