java - 如何防止 Escape 键关闭 JFace 对话框
问题描述
我希望能够拥有它,以便 Escape 键不会关闭弹出的 JFace 对话框。
在我准备的代码中,您可以通过运行 main 方法来查看此行为,当您按 Escape 时。
public class TestDialog extends Dialog
{
private Label status;
private String title;
public TestDialog(Shell parentShell, String title)
{
super(parentShell);
this.title = title;
setShellStyle(getShellStyle() & ~SWT.CLOSE);
}
@Override
protected void configureShell(Shell shell)
{
super.configureShell(shell);
shell.setText(this.title);
}
@Override
protected Control createDialogArea(Composite parent)
{
Composite composite = (Composite) super.createDialogArea(parent);
composite.setLayout(new GridLayout(1, false));
status = new Label(composite, SWT.NONE);
status.setText("Hello World");
composite.pack();
parent.pack();
return composite;
}
@Override
protected Control createButtonBar(Composite parent)
{
return parent;
}
public static void main(String[] args) {
TestDialog d = new TestDialog(new Shell(), "Test");
d.open();
}
}
解决方案
您可以向父Composite
控件添加一个键侦听器,并在其中keyEvent
匹配SWT.ESC
并编写您的自定义代码,以便在按下 ESC 键时执行您想要执行的操作。现在它将阻止 JFace 对话框关闭。
@Override
protected Control createDialogArea(final Composite parent) {
Composite composite = (Composite) super.createDialogArea(parent);
composite.setLayout(new GridLayout(1, false));
status = new Label(composite, SWT.NONE);
status.setText("Hello World");
composite.pack();
parent.pack();
composite.addKeyListener(new KeyAdapter() {
public void keyPressed(KeyEvent e) {
String string = "";
if (e.keyCode == SWT.ESC) {
string += "ESCAPE - keyCode = " + e.keyCode;
}
if (!string.isEmpty()) {
// do nothing
// here I am printing in console
System.out.println(string);
}
}
});
return composite;
}
推荐阅读
- android - VueJS 和 Cordova - 无法在 Vue 组件中检索 cordova 实例
- reactjs - 反应项目不会从数组中删除
- python - 生成从散点图的值推算的热图?
- oracle11g - ORA-00907: 缺少右括号 00907. 00000 - “缺少右括号”
- python - 试图理解元素树
- html - 如何保持两个图像很好地响应重叠?
- firebase - Firebase:当该电子邮件已用于 Google、Microsoft 等帐户时,是否可以添加新的电子邮件/密码帐户?
- tensorflow - 我怎么知道 tflite_convert 中的 --output_arrays
- kubernetes - Role/ClusterRole 更改是否需要重新启动/替换绑定到这些角色的 pod?
- python - ImportError:未为 f2py 加载库