根据这里的另一个讨论,我尝试打开一个模态视图,如下所示:
public void widgetSelected(SelectionEvent e) {
final Shell dialogShell = new Shell(ApplicationRunner.getApp()
.getShell().getDisplay(), SWT.PRIMARY_MODAL | SWT.SHEET);
dialogShell.setLayout(new FillLayout());
Button closeButton = new Button(dialogShell, SWT.PUSH);
closeButton.setText("Close");
closeButton.addSelectionListener(new SelectionListener() {
public void widgetSelected(SelectionEvent e) {
dialogShell.dispose();
}
@Override
public void widgetDefaultSelected(SelectionEvent arg0) {
// TODO Auto-generated method stub
}
});
dialogShell.setDefaultButton(closeButton);
dialogShell.addDisposeListener(new DisposeListener() {
public void widgetDisposed(DisposeEvent e) {
System.out.println("Modal dialog closed");
}
});
dialogShell.pack();
dialogShell.open();
}
它会打开所需的窗口,但不是模态的。我可以访问主 shell 并打开同一模式对话框的另一个实例。谁能指出我正确的方向?
谢谢,马库斯
最佳答案
我强烈建议通过扩展 org.eclipse.jface.dialogs.Dialog
创建您自己的 JFace 对话框,而不是创建您自己的带按钮的 shell。
Here是一个非常好的教程。
在构造函数中,您可以调用 setShellStyle(SWT.CLOSE | SWT.TITLE | SWT.BORDER | SWT.OK | SWT.APPLICATION_MODAL);
这将使该对话框完全模式化,如果您调用以您的主 shell 作为参数的构造函数。像这样:
public CheckboxDialog(Shell parentShell) {
super(parentShell);
setShellStyle(SWT.CLOSE | SWT.TITLE | SWT.BORDER | SWT.OK | SWT.APPLICATION_MODAL);
setBlockOnOpen(true);
}
其中 parentShell
是 GUI 的主外壳。
关于java - SWT模态对话框不是模态的,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12119997/