Demo Code As follows:
Copy code The Code is as follows: Package swt_jface.demo11;
Import org. Eclipse. SWT. SWT;
Import org. Eclipse. SWT. DND. Clipboard;
Import org. Eclipse. SWT. DND. rtftransfer;
Import org. Eclipse. SWT. DND. texttransfer;
Import org. Eclipse. SWT. DND. transfer;
Import org. Eclipse. SWT. DND. transferdata;
Import org. Eclipse. SWT. layout. gridlayout;
Import org. Eclipse. SWT. Widgets. display;
Import org. Eclipse. SWT. Widgets. event;
Import org. Eclipse. SWT. Widgets. listener;
Import org. Eclipse. SWT. Widgets. shell;
Import org. Eclipse. SWT. Widgets. toolbar;
Import org. Eclipse. SWT. Widgets. toolitem;
Public class copypaste {
Display display = New Display ();
Shell shell = new shell (Display );
Public copypaste (){
Shell. setlayout (New gridlayout ());
Toolbar toolbar = new toolbar (shell, SWT. Flat );
Toolitem itemcopy = new toolitem (toolbar, SWT. Push );
Toolitem itempaste = new toolitem (toolbar, SWT. Push );
Itemcopy. settext ("copy ");
Itempaste. settext ("Paste ");
itemcopy. addlistener (SWT. selection, new listener () {
Public void handleevent (event) {
clipboard = new clipboard (Display );
string plaintext = "Hello World";
string rtftext = "{\\ rtf1 \ B Hello world}";
texttransfer = texttransfer. getinstance ();
rtftransfer rfttransfer = rtftransfer. getinstance ();
clipboard. setcontents (New String [] {plaintext, rtftext}, new transfer [] {texttransfer, rfttransfer});
clipboard. dispose ();
}< BR >});
Itempaste. addlistener (SWT. Selection, new listener (){
Public void handleevent (event ){
Clipboard clipboard = new clipboard (Display );
Transferdata [] transferdatas = clipboard. getavailabletypes ();
For (INT I = 0; I <transferdatas. length; I ++ ){
If (rtftransfer. getinstance (). issupportedtype (transferdatas [I]) {
System. Out. println ("data is available in RTF format ");
Break;
}
}
String plaintext = (string) clipboard. getcontents (texttransfer. getinstance ());
String rtftext = (string) clipboard. getcontents (rtftransfer. getinstance ());
System. Out. println ("plain:" + plaintext + "\ n" + "RTF:" + rtftext );
Clipboard. Dispose ();
}
});
Shell. Pack ();
Shell. open ();
While (! Shell. isdisposed ()){
If (! Display. readanddispatch ()){
Display. Sleep ();
}
}
Display. Dispose ();
}
Public static void main (string [] ARGs ){
New copypaste ();
}
}