【问题标题】:Enable Copy/paste across different applications启用跨不同应用程序的复制/粘贴
【发布时间】:2014-08-03 15:25:48
【问题描述】:

Windows 中 SWT Text 的默认上下文菜单有几个我们不想要的选项。由于默认上下文菜单由操作系统提供且无法修改,因此我创建了一个自定义上下文菜单,其中仅对文本框进行删除、剪切、复制和粘贴等基本文本操作。

现在的问题是,当我从另一个应用程序复制文本并尝试粘贴到 TextBox 时,粘贴似乎不起作用。但是当我们在应用程序本身中复制/剪切文本时,它会起作用。

这里是复制和粘贴的代码。 粘贴动作:

private class PasteActionHandler extends Action {

    /** Creates a new instance. */
    private PasteActionHandler() {
        ...
        setEnabled(false);
    }

    @Override
    public void runWithEvent(Event event) {
        if (activeTextControl != null && !activeTextControl.isDisposed()) {
            activeTextControl.paste();
            updateActionsEnableState();
            return;
        }
    }

    /**
     * Updates the state of the Paste Action.
     */
    public void updateEnabledState() {
        if (activeTextControl != null && !activeTextControl.isDisposed()) {
            boolean canPaste = false;
            if (activeTextControl.getEditable()) {
                Clipboard clipboard = new Clipboard(activeTextControl.getDisplay());
                TransferData[] td = clipboard.getAvailableTypes();
                for (int i = 0; i < td.length; ++i) {
                    if (TextTransfer.getInstance().isSupportedType(td[i])) {
                        canPaste = true;
                        break;
                    }
                }
                clipboard.dispose();
            }
            setEnabled(canPaste);
            return;
        }
        setEnabled(false);
    }
}

复制操作:

private class CopyActionHandler extends Action {

    private CopyActionHandler() {
        ...
        setEnabled(false);
    }

    @Override
    public void runWithEvent(Event event) {
        if (activeTextControl != null && !activeTextControl.isDisposed()) {
            activeTextControl.copy();
            updateActionsEnableState();
            return;
        }
    }

    /**
     * Updates the state of the {@link Action}.
     */
    public void updateEnabledState() {
        if (activeTextControl != null && !activeTextControl.isDisposed()) {
            setEnabled(activeTextControl.getSelectionCount() > 0);
            return;
        }
        setEnabled(false);
    }
}

如您所见,我调用 Text 控件的复制和粘贴函数来执行这些操作。 还有在 SWT 中有没有办法获取系统剪贴板?

【问题讨论】:

    标签: java textbox swt clipboard copy-paste


    【解决方案1】:

    要从 SWT 复制剪贴板中的内容,我使用以下代码:

        String textData = textControl.getText();
        if (textData.length() > 0) {
          org.eclipse.swt.dnd.Clipboard cb = new org.eclipse.swt.dnd.Clipboard(display);
          org.eclipse.swt.dnd.TextTransfer textTransfer = org.eclipse.swt.dnd.TextTransfer.getInstance();
          cb.setContents(new Object[]{textData}, new Transfer[]{textTransfer});
        }
    

    你可以试试这个,而不是 activeTextControl.copy() 函数,看看它是否有效

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-06-03
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多