等待非模态窗口关闭以恢复代码



我有一个程序,在某些情况下需要打开一个对话框"anDialog";它包含打开更多对话框的按钮";B";以及";C";,这些子对话框中的一些不是模态的,所以如果我设置对话框";anDialog";作为模态,它在打开后立即上升到它们上方并挡住它们。但是如果我设置";anDialog";由于不是模态的,调用它的类一直在运行,它不应该运行。

调用对话框";A";

OpenAttributesAssistentCommand attrAssistent = new OpenAttributesAssistentCommand((InternalInterfaceAttributes) parent, transcriptor);
attrAssistent.execute();
//... more stuff after

执行

public void execute() {
AttributesAssistentDialog anDialog = new AttributesAssistentDialog(intFrame, transcriptor);
anDialog.setVisible(true);
}

我希望呼叫者等待对话";anDialog";在继续跑步之前完成。如果它能理解关闭和btnOk之间的区别,那就太好了。此外,如果有一种方法可以使它不阻塞非模态子级,那也没问题

我相信你可以用SecondaryLoop来实现这一点,它阻塞线程而不阻塞用户界面,直到调用其exit方法:

private SecondaryLoop attrAssistantLoop;
// ...
OpenAttributesAssistentCommand attrAssistent =
new OpenAttributesAssistentCommand(
(InternalInterfaceAttributes) parent, transcriptor);
attrAssistantLoop = Toolkit.getSystemEventQueue().createSecondaryLoop();
attrAssistent.execute(attrAssistantLoop);
attrAssistantLoop.enter();  // Wait for dialog to close
// OpenAttributesAssistentCommand class
public void execute(SecondaryLoop loop) {
AttributesAssistentDialog anDialog = new AttributesAssistentDialog(intFrame, transcriptor);
anDialog.addWindowListener(new WindowAdapter() {
@Override
public void windowClosed(WindowEvent event) {
if (loop != null) {
loop.exit();  // Allow waiting code to proceed
}
}
});
anDialog.setVisible(true);
}

最新更新