java – 如何检测SWT对话框是否已打开且可见?
发布时间:2020-12-15 08:43:28 所属栏目:Java 来源:网络整理
导读:我有一个包含许多页面的SWT WizardDialog.当这个对话框第一次打开时,我必须检查一些条件,如果满足这些条件,我需要在新打开的对话框上显示一个弹出窗口. 所以我有这个代码来监听SWT.Show事件.事件监听器响应SWT.Show进行测试并显示一个消息框: final WizardD
我有一个包含许多页面的SWT WizardDialog.当这个对话框第一次打开时,我必须检查一些条件,如果满足这些条件,我需要在新打开的对话框上显示一个弹出窗口.
所以我有这个代码来监听SWT.Show事件.事件监听器响应SWT.Show进行测试并显示一个消息框: final WizardDialog dialog = new WizardDialog(shell,wizard); dialog.setTitle("New Wizard"); dialog.create(); dialog.getShell().addListener(SWT.Show,new Listener() { private boolean firstShowing = true; @Override public void handleEvent(Event event) { if (firstShowing && someConditionExists()) { MessageBox messageBox = new MessageBox(dialog.getShell(),SWT.OK | SWT.ICON_WARNING); messageBox.setMessage("Test"); messageBox.open(); firstShowing = false; } } }); dialog.open(); 除了它太快了!调用处理程序时,该对话框不可见.在对话框可见之前出现我的消息框,只有在我关闭消息框时才会出现对话框. 很明显SWT.Show是不可靠的,至少在我运行它的Windows上.我也尝试在激活时将此代码放入ShellListener中,但这种情况甚至在上面的SWT.Show示例之前就已经发生了. 那么当对话框可见时,如何可靠地显示消息框? Plan B是一个基于脏计时器的hack,其中一个计时器被设置为在未来200ms触发,并希望它在对话框可见时触发,但显然这可能会引入它自己的问题. 解决方法
我在类似情况下使用(需要在应用程序窗口可见后调用appStarted())如下所示.
public class App extends ApplicationWindow { @Override protected Control createContents(Composite parent) { // ... getShell().addShellListener(new ShellAdapter() { @Override public void shellActivated(ShellEvent shellevent) { if (!started) { Shell s = (Shell) shellevent.getSource(); s.setVisible(true); appStarted(); started = true; } } }); } } 也许你可以像下面这样使用: final WizardDialog dialog = new WizardDialog(shell,wizard); dialog.setTitle("New Wizard"); dialog.create(); dialog.getShell().addShellListener(new ShellAdapter() { @Override public void shellActivated(ShellEvent shellevent) { if (firstShowing && someConditionExists()) { Shell s = (Shell) shellevent.getSource(); s.setVisible(true); MessageBox messageBox = new MessageBox(dialog.getShell(),SWT.OK | SWT.ICON_WARNING); messageBox.setMessage("Test"); messageBox.open(); firstShowing = false; } } }); dialog.open(); (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |