[英]In SWT, what's the best way to communicate between a parent shell and a non dialog child shell?
我有一個用SWT編寫的大型現有應用程序,我必須進行修改。
GUI包括通過外殼打開非Dialog子外殼。
現在,當子外殼關閉時,我必須更新有關父外殼的信息。
我想象了兩個選擇:
如果在父代碼中我可以偵聽子外殼事件並根據子外殼上發生的事情采取措施,那就太好了。 這是一種可觀察的模式。 我在“ SWT:開發人員筆記本”中閱讀:
ChildShell不需要事件循環。 為什么? 因為父級外殼的事件循環處理父級中打開的所有對象的事件分配。 子級保持打開狀態,直到被用戶關閉或父級關閉為止。
我沒有在SWT中進行實驗,而且示例很少。 那么,SWT的實現方式是什么? 我可以將父循環用於此目的嗎?
謝謝。
我建議您在子外殼上使用ShellListener
。 然后,您可以覆蓋shellClosed
方法。
例
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.events.ShellEvent;
import org.eclipse.swt.events.ShellListener;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Text;
public class Test {
private static Text text;
public static void main (String [] args)
{
Display display = new Display ();
final Shell shell = new Shell (display);
shell.setLayout(new GridLayout());
shell.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true));
shell.setSize(200, 100);
shell.setText("Parent Shell");
Label label = new Label(shell, SWT.NONE);
label.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, false));
label.setText("The text from child shell ...");
text = new Text(shell, SWT.BORDER);
text.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, false));
Button openChild = new Button(shell, SWT.PUSH);
openChild.setText("Open Child ...");
openChild.addSelectionListener(new SelectionAdapter() {
public void widgetSelected(SelectionEvent e) {
openChild(shell);
}
});
shell.open ();
while (!shell.isDisposed ()) {
if (!display.readAndDispatch ()) display.sleep ();
}
display.dispose ();
}
private static void openChild(Shell parent)
{
final Shell dialog = new Shell (parent, SWT.DIALOG_TRIM);
dialog.setLayout(new GridLayout());
dialog.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true));
dialog.setSize(200, 100);
dialog.setText("Child Shell");
Label childLabel = new Label(dialog, SWT.NONE);
childLabel.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, false));
childLabel.setText("Type something here ...");
final Text childText = new Text(dialog, SWT.BORDER);
childText.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, false));
Button okButton = new Button (dialog, SWT.PUSH);
okButton.setText ("&OK");
okButton.addSelectionListener(new SelectionAdapter() {
public void widgetSelected(SelectionEvent e) {
dialog.close();
}
});
dialog.addShellListener(new ShellListener() {
public void shellIconified(ShellEvent e) {
}
public void shellDeiconified(ShellEvent e) {
}
public void shellDeactivated(ShellEvent e) {
}
public void shellClosed(ShellEvent e) {
if(text != null && !text.isDisposed())
text.setText(childText.getText());
}
public void shellActivated(ShellEvent e) {
}
});
dialog.setDefaultButton (okButton);
dialog.open ();
}
}
注意
您也可以使用DisposeListener
但是在這種情況下,不能使用text.setText(childText.getText());
(請參見上面的示例)。 要處理此問題,請將文本保存在String變量中,然后使用string變量填充父級文本框。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.