我在 Linux 机器上工作,与 Windows 不同,有两个剪贴板,STRG-C 和 STRG-V 剪贴板以及突出显示文本并通过鼠标中键粘贴。
现在我 try catch 粘贴操作,因为我想验证剪贴板并防止文本字段非法输入。
我成功地捕获了 STRG-V 的标准粘贴:
Action action = textField.getActionMap().get("paste-from-clipboard");
textField.getActionMap().put("paste-from-clipboard", new ProxyAction(action, "paste-from-clipboard"));
以及ProxyAction类的实现:
public class ProxyAction extends TextAction
{
private Action action;
public ProxyAction1(Action action, String actionName)
{
super(actionName);
this.action = action;
}
@Override
public void actionPerformed(ActionEvent e)
{
action.actionPerformed(e);
}
}
我的问题是,我怎样才能捕获Linux特定的鼠标中键粘贴? 上面的代码没有得到它,并且来自 jTextfield 的“粘贴”操作也没有解决问题。
最佳答案
i want to validate the clipboard and prevent the textfield from illegal input.
您可以使用DocumentFilter
,而不是 try catch 粘贴键。在将任何文本插入到 Document
之前,会调用 DocumentFilter
。
无论文本是在文本字段中输入还是粘贴到文本字段中,都会调用此过滤器。
查看 Swing 教程中 How to Implement a DocumentFilter 的部分了解更多信息。
关于java - 通过鼠标中键捕获粘贴操作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35437215/