Java 使用 SWT 显示父模态对话框
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4370452/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Display parent modal dialog with SWT
提问by Mot
AWT/Swing allows to show application modal (blocking the whole application) and parent modal (blocking only the parents) dialogs. How can I achieve the same with SWT?
AWT/Swing 允许显示应用程序模式(阻止整个应用程序)和父模式(仅阻止父级)对话框。我怎样才能用 SWT 达到同样的效果?
采纳答案by Jean-Philippe Pellet
In order to block the whole application, you can create the dialog Shell
with the style SWT.APPLICATION_MODAL
, open it, and then pump the UI events until the shell is disposed:
为了阻止整个应用程序,您可以创建Shell
带有样式的对话框SWT.APPLICATION_MODAL
,打开它,然后泵送 UI 事件,直到 shell 被释放:
Display display = Display.getDefault();
Shell dialogShell = new Shell(display, SWT.APPLICATION_MODAL);
// populate dialogShell
dialogShell.open();
while (!dialogShell.isDisposed()) {
if (!display.readAndDispatch()) {
display.sleep();
}
}
If you want to block input only to the parent, try using the style SWT.PRIMARY_MODAL
, though the Javadocs specify (as for the other modal styles) that this is a hint; i.e., that different SWT implementations may not exactly handle it the same way. Likewise, I don't know of an implementation that would honor the SWT.SYSTEM_MODAL
style.
如果您只想阻止对父级的输入,请尝试使用 style SWT.PRIMARY_MODAL
,尽管 Javadocs 指定(对于其他模式样式)这是一个提示;即,不同的 SWT 实现可能不会完全以相同的方式处理它。同样,我不知道会尊重这种SWT.SYSTEM_MODAL
风格的实现。
UPDATE: Answer to first comment
更新:回答第一条评论
If you have two or more primary modals open at the same time, you cannot use the tricks to pump the events until the modal is closed, as they could be closed in any order. The code will run, but execution will resume after the while loop after the current dialog is closed and all other such dialogs that have been opened after it. In this case, I would register a DisposeListener
on each dialog to get a callback when they are closed. Something like this:
如果您同时打开了两个或多个主要模态,则在模态关闭之前您不能使用这些技巧来抽取事件,因为它们可以按任何顺序关闭。代码将运行,但在当前对话框关闭以及在此之后打开的所有其他此类对话框后,将在 while 循环后恢复执行。在这种情况下,我会DisposeListener
在每个对话框上注册一个以在它们关闭时获得回调。像这样的东西:
void run() {
Display display = new Display();
Shell shell1 = openDocumentShell(display);
Shell shell2 = openDocumentShell(display);
// close both shells to exit
while (!shell1.isDisposed() || !shell2.isDisposed()) {
if (!display.readAndDispatch()) {
display.sleep();
}
}
display.dispose();
}
Shell openDocumentShell(final Display display) {
final Shell shell = new Shell(display, SWT.SHELL_TRIM);
shell.setLayout(new FillLayout());
Button button = new Button(shell, SWT.PUSH);
button.setText("Open Modal Dialog");
button.addSelectionListener(new SelectionAdapter() {
@Override
public void widgetSelected(SelectionEvent e) {
System.out.println("Button pressed, about to open modal dialog");
final Shell dialogShell = new Shell(shell, SWT.PRIMARY_MODAL | SWT.SHEET);
dialogShell.setLayout(new FillLayout());
Button closeButton = new Button(dialogShell, SWT.PUSH);
closeButton.setText("Close");
closeButton.addSelectionListener(new SelectionAdapter() {
@Override
public void widgetSelected(SelectionEvent e) {
dialogShell.dispose();
}
});
dialogShell.setDefaultButton(closeButton);
dialogShell.addDisposeListener(new DisposeListener() {
@Override
public void widgetDisposed(DisposeEvent e) {
System.out.println("Modal dialog closed");
}
});
dialogShell.pack();
dialogShell.open();
}
});
shell.pack();
shell.open();
return shell;
}