我正在使用 SWT JFace 对话框。
我向“确定”按钮添加了一个监听器,我想在用户单击“确定”按钮后显示一个消息框。 此步骤中的问题是,当我单击“确定”按钮时,外壳就会被释放。我怎样才能防止这种行为?
最佳答案
以下代码将阻止通过“确定”按钮关闭对话框。只是不要在 okPressed()
方法中调用 this.close()
即可:
public static void main(String[] args) {
final Display display = new Display();
final Shell shell = new Shell(display);
shell.setLayout(new FillLayout());
new OptionsDialog(shell).open();
shell.pack();
shell.open();
while (!shell.isDisposed()) {
if (!display.readAndDispatch()) {
display.sleep();
}
}
display.dispose();
}
private static class OptionsDialog extends Dialog {
private Composite composite;
public OptionsDialog(Shell parentShell)
{
super(parentShell);
setShellStyle(parentShell.getStyle() | SWT.CLOSE | SWT.TITLE | SWT.BORDER | SWT.APPLICATION_MODAL);
setBlockOnOpen(true);
}
protected Control createDialogArea(Composite parent) {
this.composite = (Composite) super.createDialogArea(parent);
GridLayout layout = new GridLayout(1, false);
layout.marginHeight = 5;
layout.marginWidth = 10;
composite.setLayout(layout);
createContent();
return composite;
}
private void createContent()
{
/* add your widgets */
}
protected void configureShell(Shell newShell)
{
super.configureShell(newShell);
newShell.setText("Shell name");
}
public void okPressed()
{
/* DO NOTHING HERE!!! */
//this.close();
}
}
关于java - 按 OK 按钮后 JFace 对话框保持打开状态,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12337652/