[英]Display parent modal dialog with SWT
AWT / Swing允許顯示應用程序模式(阻止整個應用程序)和父模式(僅阻止父項)對話框。 如何通過SWT實現同樣的目標?
為了阻止整個應用程序,您可以使用樣式SWT.APPLICATION_MODAL
創建對話框Shell
,打開它,然后泵送UI事件,直到shell被SWT.APPLICATION_MODAL
:
Display display = Display.getDefault();
Shell dialogShell = new Shell(display, SWT.APPLICATION_MODAL);
// populate dialogShell
dialogShell.open();
while (!dialogShell.isDisposed()) {
if (!display.readAndDispatch()) {
display.sleep();
}
}
如果您只想阻止對父項的輸入,請嘗試使用樣式SWT.PRIMARY_MODAL
,盡管Javadocs指定(對於其他模式樣式)這是一個提示; 也就是說,不同的SWT實現可能不會以相同的方式完全處理它。 同樣,我不知道一個符合SWT.SYSTEM_MODAL
樣式的實現。
更新:回答第一條評論
如果您同時打開兩個或更多主要模態,則在模態關閉之前不能使用技巧來抽取事件,因為它們可以按任何順序關閉。 代碼將運行,但在當前對話框關閉后的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;
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.