Copy and Paste : Clipboard « SWT JFace Eclipse « Java

Java
1. 2D Graphics GUI
2. 3D
3. Advanced Graphics
4. Ant
5. Apache Common
6. Chart
7. Class
8. Collections Data Structure
9. Data Type
10. Database SQL JDBC
11. Design Pattern
12. Development Class
13. EJB3
14. Email
15. Event
16. File Input Output
17. Game
18. Generics
19. GWT
20. Hibernate
21. I18N
22. J2EE
23. J2ME
24. JDK 6
25. JNDI LDAP
26. JPA
27. JSP
28. JSTL
29. Language Basics
30. Network Protocol
31. PDF RTF
32. Reflection
33. Regular Expressions
34. Scripting
35. Security
36. Servlets
37. Spring
38. Swing Components
39. Swing JFC
40. SWT JFace Eclipse
41. Threads
42. Tiny Application
43. Velocity
44. Web Services SOA
45. XML
Java Tutorial
Java Source Code / Java Documentation
Java Open Source
Jar File Download
Java Articles
Java Products
Java by API
Photoshop Tutorials
Maya Tutorials
Flash Tutorials
3ds-Max Tutorials
Illustrator Tutorials
GIMP Tutorials
C# / C Sharp
C# / CSharp Tutorial
C# / CSharp Open Source
ASP.Net
ASP.NET Tutorial
JavaScript DHTML
JavaScript Tutorial
JavaScript Reference
HTML / CSS
HTML CSS Reference
C / ANSI-C
C Tutorial
C++
C++ Tutorial
Ruby
PHP
Python
Python Tutorial
Python Open Source
SQL Server / T-SQL
SQL Server / T-SQL Tutorial
Oracle PL / SQL
Oracle PL/SQL Tutorial
PostgreSQL
SQL / MySQL
MySQL Tutorial
VB.Net
VB.Net Tutorial
Flash / Flex / ActionScript
VBA / Excel / Access / Word
XML
XML Tutorial
Microsoft Office PowerPoint 2007 Tutorial
Microsoft Office Excel 2007 Tutorial
Microsoft Office Word 2007 Tutorial
Java » SWT JFace Eclipse » ClipboardScreenshots 
Copy and Paste
Copy and Paste


/******************************************************************************
 * All Right Reserved. 
 * Copyright (c) 1998, 2004 Jackwind Li Guojie
 
 * Created on 2004-4-28 10:43:40 by JACK
 * $Id$
 
 *****************************************************************************/
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 event) {
        Clipboard clipboard = new Clipboard(display);
        String plainText = "Hello World";
        String rtfText = "{\\rtf1\\b Hello World}";
        TextTransfer textTransfer = TextTransfer.getInstance();
        RTFTransfer rftTransfer = RTFTransfer.getInstance();
        clipboard.setContents(new String[]{plainText, rtfText}new Transfer[]{textTransfer, rftTransfer});
        clipboard.dispose();
      }
    });
    
    itemPaste.addListener(SWT.Selection, new Listener() {
      public void handleEvent(Event event) {
        Clipboard clipboard = new Clipboard(display);
        
        TransferData[] transferDatas = clipboard.getAvailableTypes();

        for(int i=0; i<transferDatas.length; i++) {
          // Checks whether RTF format is available.
          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();
    //textUser.forceFocus();

    // Set up the event loop.
    while (!shell.isDisposed()) {
      if (!display.readAndDispatch()) {
        // If no more entries in event queue
        display.sleep();
      }
    }

    display.dispose();
  }

  private void init() {

  }

  public static void main(String[] args) {
    new CopyPaste();
  }
}


           
       
Related examples in the same category
1. SWT Clipboard ExampleSWT Clipboard Example
2. Clipboard Composite
3. Enable/disable menu depending on clipboard content availabilityEnable/disable menu depending on clipboard content availability
4. Copy and paste data with the clipboardCopy and paste data with the clipboard
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.