【发布时间】: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