【问题标题】:How to stop JFace dialog from closing after dialog.open()如何在 dialog.open() 之后停止 JFace 对话框关闭
【发布时间】:2014-03-12 05:36:23
【问题描述】:

我有一个 JFace 对话框,这就是我打开的方式:

SampleDialog dialog = new SampleDialog(shell);
if(Window.OK == dialog.open())
{
  // do something
  if(condition)
  {
    MessageBox dialog = new MessageBox(shell, SWT.ICON_QUESTION | SWT.OK| SWT.CANCEL);
    dialog.setText("Sample");
    dialog.setMessage("Some problem in processing");
  }
  else
  {
     // do something
  }
}

如果控件转到 if 块,我不希望关闭对话框。如何做到这一点?

【问题讨论】:

  • 一旦dialog.open() 被调用,对话框就会打开。除非用户单击对话框上的关闭(X)、确定或取消按钮,否则控件将永远不会到达if statement。请更新帖子为什么您需要这种行为或您想要实现什么?

标签: java dialog swt jface


【解决方案1】:

您应该通过覆盖对话框中的okPressed 方法来进行检查和消息框显示。

public class SampleDialog extends Dialog
{

  @Override
  protected void okPressed()
  {
    if (condition)
     {
        // TODO show your message box
     }
    else
     {
       // Allow the dialog to close
       super.okPressed();
     } 
  }

}

【讨论】:

    【解决方案2】:

    覆盖 JFace Dialogs close() 方法是另一种解决方法:

    @Override
    public boolean close()
    {
        if( condition )
        {
            if( this.getReturnCode() == OK )
            {
                MessageBox dialog = new MessageBox(shell, SWT.ICON_QUESTION | SWT.OK|                   SWT.CANCEL);
                dialog.setText("Sample");
                dialog.setMessage("Some problem in processing");
    
                return false;
            }
        }
        return super.close();
    }
    

    【讨论】:

      猜你喜欢
      • 2018-02-17
      • 2016-11-17
      • 2020-06-18
      • 1970-01-01
      • 1970-01-01
      • 2020-12-23
      • 1970-01-01
      • 2012-08-22
      • 1970-01-01
      相关资源
      最近更新 更多