防止 SWT shell 处理
Preventing SWT shell from disposing
我用这个实现创建了一个 SWT 对话框:
public class FindDialog extends Dialog {
private DialogResult result;
private Display display;
private Shell shell;
private Button okayButton;
/*...*/
public FindDialog(Shell parent) {
this(parent, SWT.APPLICATION_MODAL | SWT.DIALOG_TRIM);
}
public FindDialog(Shell parent, int style) {
super(parent, style);
display = getParent().getDisplay();
initUI();
}
public DialogResult open() {
result = DialogResult.Cancel;
shell.open();
while (shell.isVisible()) {
if (!display.readAndDispatch()) {
display.sleep();
}
}
return result;
}
private void initUI() {
shell = new Shell(getParent(), getStyle());
shell.addListener(SWT.Close, new ShellClosingHandler());
okayButton = new Button(shell, SWT.PUSH);
okayButton.addListener(SWT.Selection, new ButtonClickedHandler());
/*...*/
}
private class ButtonClickedHandler implements Listener {
public void handleEvent(Event e) {
Button button = (Button)e.widget;
if (button == okayButton) {
result = DialogResult.OK;
shell.close();
}
}
}
private class ShellClosingHandler implements Listener {
public void handleEvent(Event e) {
if (result == DialogResult.OK) {
e.doit = validate();
}
}
private boolean validate() {
/*...*/
}
}
}
还有一些文本字段、按钮和复选框,但我认为这对我的问题并不重要。
对话框 window 正确弹出,我可以毫无问题地在 GUI 上进行更改。
最后我点击确定按钮,然后发生以下情况:
SWT.Close 事件正在触发,我的验证方法被调用并根据结果关闭对话框。到目前为止没问题。
但是 shell.close() 不仅关闭了对话框,它还处理了 shell。问题就在这里,因为
我不想在每次调用 open 方法时都重建对话框 GUI。我在程序启动时创建所有对话框,然后只在需要时打开和关闭它。
在主程序的不同位置关闭对话框后,我需要一些文本字段的值或复选框的状态。因此,持有对话框对象的引用并实现一些 getter 来提取数据似乎是个好主意。但是如果 shell 被处置了,我就没有机会得到信息了。
如果 shell 被释放,那么我将松开对话框的 "state",所以我必须在下次显示对话框时重新填充它。
所以我的问题是:是否有可能阻止 shell 处理?
还是我忽略了另一个概念,所以我不必重新构建完整的对话集?
if (button == okayButton) {
result = DialogResult.OK;
shell.setVisible(false);
}
您可以使用 setVisible(false) 而不是关闭
所以它会被隐藏起来,不会被处置。
隐藏后可以得到文本框的值
无需再次重建
隐藏后文本框里的过去的值还在。
我用这个实现创建了一个 SWT 对话框:
public class FindDialog extends Dialog {
private DialogResult result;
private Display display;
private Shell shell;
private Button okayButton;
/*...*/
public FindDialog(Shell parent) {
this(parent, SWT.APPLICATION_MODAL | SWT.DIALOG_TRIM);
}
public FindDialog(Shell parent, int style) {
super(parent, style);
display = getParent().getDisplay();
initUI();
}
public DialogResult open() {
result = DialogResult.Cancel;
shell.open();
while (shell.isVisible()) {
if (!display.readAndDispatch()) {
display.sleep();
}
}
return result;
}
private void initUI() {
shell = new Shell(getParent(), getStyle());
shell.addListener(SWT.Close, new ShellClosingHandler());
okayButton = new Button(shell, SWT.PUSH);
okayButton.addListener(SWT.Selection, new ButtonClickedHandler());
/*...*/
}
private class ButtonClickedHandler implements Listener {
public void handleEvent(Event e) {
Button button = (Button)e.widget;
if (button == okayButton) {
result = DialogResult.OK;
shell.close();
}
}
}
private class ShellClosingHandler implements Listener {
public void handleEvent(Event e) {
if (result == DialogResult.OK) {
e.doit = validate();
}
}
private boolean validate() {
/*...*/
}
}
}
还有一些文本字段、按钮和复选框,但我认为这对我的问题并不重要。
对话框 window 正确弹出,我可以毫无问题地在 GUI 上进行更改。
最后我点击确定按钮,然后发生以下情况:
SWT.Close 事件正在触发,我的验证方法被调用并根据结果关闭对话框。到目前为止没问题。
但是 shell.close() 不仅关闭了对话框,它还处理了 shell。问题就在这里,因为
我不想在每次调用 open 方法时都重建对话框 GUI。我在程序启动时创建所有对话框,然后只在需要时打开和关闭它。
在主程序的不同位置关闭对话框后,我需要一些文本字段的值或复选框的状态。因此,持有对话框对象的引用并实现一些 getter 来提取数据似乎是个好主意。但是如果 shell 被处置了,我就没有机会得到信息了。
如果 shell 被释放,那么我将松开对话框的 "state",所以我必须在下次显示对话框时重新填充它。
所以我的问题是:是否有可能阻止 shell 处理? 还是我忽略了另一个概念,所以我不必重新构建完整的对话集?
if (button == okayButton) {
result = DialogResult.OK;
shell.setVisible(false);
}
您可以使用 setVisible(false) 而不是关闭
所以它会被隐藏起来,不会被处置。
隐藏后可以得到文本框的值
无需再次重建
隐藏后文本框里的过去的值还在。