SWT(JFace)体验之复制粘贴

演示代码如下:


代码如下:

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

(0)

相关推荐

  • SWT(JFace)体验之复制粘贴

    演示代码如下: 复制代码 代码如下: 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.sw

  • SWT(JFace)体验之GridLayout布局

    GridLayout布局 GridLayout 布局的功能非常强大,也是笔者常用的一种布局方式.GridLayout是网格式布局,它把父组件分成一个表格,默认情况下每个子组件占据一个单元格的空间,每个子组件按添加到父组件的顺序排列在表格中.GridLayout提供了很多的属性,可以灵活设置网格的信息.另外,GridLayout 布局提供了GridData类,子组件可以设置相应的GridData,例如 "dogPhoto.setLayoutData(gridData)",GridData

  • SWT(JFace)体验之StyledText类

    WrapLines.java 复制代码 代码如下: package swt_jface.demo4; import org.eclipse.swt.SWT; import org.eclipse.swt.layout.GridData; import org.eclipse.swt.layout.GridLayout; import org.eclipse.swt.widgets.Display; import org.eclipse.swt.widgets.Label; import org.

  • SWT(JFace)体验之ApplicationWindow

    测试代码如下: 复制代码 代码如下: package swt_jface.demo; import org.eclipse.jface.window.ApplicationWindow; import org.eclipse.swt.SWT; import org.eclipse.swt.events.ModifyEvent; import org.eclipse.swt.events.ModifyListener; import org.eclipse.swt.layout.GridLayou

  • SWT(JFace) 体验之FontRegistry

    复制代码 代码如下: package swt_jface.demo; import org.eclipse.jface.resource.FontRegistry; import org.eclipse.swt.SWT; import org.eclipse.swt.graphics.FontData; import org.eclipse.swt.layout.GridData; import org.eclipse.swt.layout.GridLayout; import org.ecli

  • SWT(JFace)体验之图片的动态渐变效果

    1.渐变: 复制代码 代码如下: package swt_jface.demo10; import org.eclipse.swt.SWT; import org.eclipse.swt.events.PaintEvent; import org.eclipse.swt.events.PaintListener; import org.eclipse.swt.graphics.Image; import org.eclipse.swt.graphics.ImageData; import org

  • SWT(JFace)体验之ViewForm的使用

    代码如下: 复制代码 代码如下: package swt_jface.demo9; import org.eclipse.swt.SWT; import org.eclipse.swt.custom.ViewForm; import org.eclipse.swt.layout.FillLayout; import org.eclipse.swt.widgets.Display; import org.eclipse.swt.widgets.Label; import org.eclipse.s

  • SWT(JFace)体验之Sash(活动控件)

    演示代码如下: 复制代码 代码如下: package swt_jface.demo9; import org.eclipse.swt.SWT; import org.eclipse.swt.widgets.Display; import org.eclipse.swt.widgets.Event; import org.eclipse.swt.widgets.Listener; import org.eclipse.swt.widgets.Sash; import org.eclipse.swt

  • SWT(JFace)体验之Slider,Scale

    Slider: 复制代码 代码如下: package swt_jface.demo8; import org.eclipse.swt.SWT; import org.eclipse.swt.layout.GridData; import org.eclipse.swt.layout.GridLayout; import org.eclipse.swt.widgets.Display; import org.eclipse.swt.widgets.Event; import org.eclipse

  • SWT(JFace)体验之ProgressBar

    先看代码: ProgressBarExamples.java 复制代码 代码如下: package swt_jface.demo8; import org.eclipse.swt.SWT; import org.eclipse.swt.events.PaintEvent; import org.eclipse.swt.events.PaintListener; import org.eclipse.swt.graphics.Font; import org.eclipse.swt.graphic

随机推荐