[英]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.