简体   繁体   English

SWT-MessageDialog-外壳

[英]SWT - MessageDialog - Shells

I have an operations class that has no gui. 我有一个没有gui的操作类。 The class basically does data management. 该类基本上进行数据管理。 The class is called from a method in my Main GUI. 从我的主GUI中的方法调用该类。 The problem I am having is with displaying messages to the user if something fails. 我遇到的问题是如果出现故障则向用户显示消息。 I am using MessageDialog , but it keeps failing at runtime. 我正在使用MessageDialog ,但它在运行时始终失败。 I think the issue is with Shell. 我认为问题出在壳牌公司。 When I try to use null as the shell. 当我尝试使用null作为外壳程序时。

MessageDialog.openError(null, "Printer Error Message", "Error getting print reply file.");

The error is null pointer exception 错误为空指针异常

MessageDialog.openError(Display.getCurrent().getActiveShell() etc

The error is null pointer exception 错误为空指针异常

MessageDialog.openError(Display.getDefault().getActiveShell()

The error is invalid thread access 错误是无效的线程访问

  1. Being this is not a GUI class, do I have to pass in the shell from the GUI parent? 由于这不是GUI类,我是否必须从GUI父级传递外壳程序?
  2. Can I just create a shell in the class and then use that? 我可以只在类中创建一个shell然后使用它吗?

You can fix the ERROR_THREAD_INVALID_ACCESS error with Display.syncExec or Display.asyncExec . 您可以使用Display.syncExecDisplay.asyncExec修复ERROR_THREAD_INVALID_ACCESS错误。 Try with: 尝试:

Display.syncExec(new Runnable() {
    void run() {
        MessageDialog.openError(Display.getDefault().getActiveShell()...
    }
}

This will do what you want: 这将做您想要的:

MessageDialog.openError(new Shell(), "Printer Error Message", "Error getting print reply file.");

Just create a new Shell and pass it to the MessageDialog . 只需创建一个新的Shell并将其传递给MessageDialog

Few important points to consider. 需要考虑的几个重点。

  1. First of all, do not mix Data Management classes(models) with UI. 首先,请勿将数据管理类(模型)与UI混合使用。
  2. Have a utility class and methods to show errors/info messages. 有一个实用程序类和方法来显示错误/信息消息。
  3. always access UI widgets in UI thread. 始终在UI线程中访问UI小部件。 Use Display.getDefault().asyncExec() or syncExce() 使用Display.getDefault().asyncExec()syncExce()
  4. Check Display.getDefault().getActiveShell() to pass it to the dialog first, if it is null, create one and pass it. 首先检查Display.getDefault().getActiveShell()以将其传递给对话框,如果为null,则创建一个并传递给对话框。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM