按OK按钮后,JFace对话框保持打开状态

时间:2012-09-09 08:36:31

标签: java swt

我正在使用SWT JFace对话框。

我在OK按钮上添加了一个监听器,一旦用户点击OK按钮,我想显示一个消息框。 此步骤中的问题是,当我单击“确定”按钮时,shell将被释放。我怎么能防止这种行为?

1 个答案:

答案 0 :(得分:2)

以下代码将阻止通过“确定”按钮关闭对话框。只是不要在this.close()方法中调用okPressed()

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();
    }
}