shell SWT -仅允许打开1个对话框

yyyllmsg  于 2023-03-13  发布在  Shell
关注(0)|答案(3)|浏览(139)

我在基本对话框中有一个按钮,用于打开结果对话框。

private void showPlotResultsDialog() {
  resultsDialog = new AplotPlotResultsDialog(getShell());
  resultsDialog.setBlockOnOpen(true);
  resultsDialog.open();

}
用户被允许在工作时让结果对话框保持打开状态。但是我最近注意到用户可以随意多次点击“打开结果对话框”。
每次点击都会打开一个新的结果对话框。可以打开几个相同的对话框,但表中的数据不同。
1.当用户点击按钮时,是否可以检查并查看对话框是否已经打开?如果已经打开,弹出消息,说明对话框已经打开并阻止打开新对话框。

jmp7cifd

jmp7cifd1#

当他们点击按钮时,是否可以检查并查看对话框是否已经打开?
当然可以。只要在你的方法中检查null。如果示例不为空,那么对话框已经打开。
如果一个已经打开,弹出一条消息,说明它已经打开并阻止打开新的
更新对话框并将焦点设置在对话框上会更好。2节省了用户关闭弹出消息,关闭对话框,然后打开同一个对话框的时间。

ajsxfq5m

ajsxfq5m2#

另一种可能性是使用以下方法给予您的shell(应该只打开一次)提供一个唯一ID:
shell.setData("yourID");
例如,如果您有一个SelectionListener,则可以检查ID为yourIDShell是否已经打开。
行动:

  • 如果Shell在某处打开:激活 shell (设置焦点)
  • 如果Shell未打开:打开 shell

示例(见备注):

yourButton.addSelectionListener(new SelectionAdapter() {
    @Override
    public void widgetSelected(SelectionEvent e) {

        // Loop through all active shells and check if 
        // the shell is already open
        Shell[] shells = Display.getCurrent().getShells();

        for(Shell shell : shells) {
            String data = (String) shell.getData();

            // only activate the shell and return
            if(data != null && data.equals("yourID")) {
                shell.setFocus();
                return;
            }
        }

        // open the shell and the dialog
        Shell shell = new Shell(Display.getCurrent());
        shell.setData("yourID");
        YourDialog yourDialog = new YourDialog(shell);
        yourDialog.open();
    }
});
6l7fqoea

6l7fqoea3#

对话框只创建一次,并在监听器回调中使用。不要在回调中初始化对话框。
例如:

MyDialog viewer = new MyDialog(getShell()); // initiated once
myToolItem.addSelectionListener(new SelectionAdapter() {
    @Override
    public void widgetSelected(SelectionEvent e) {
        if (shellCommand != null) {
            viewer.setTitle("My Title");
            viewer.setContent("My content");
            viewer.open(); // called to focus if already opened
        }
    }
});

相关问题