等待jdialog关闭
我有一个类 FilePathDialog,它扩展了 JDialog,并且该类正在从某个类 X 调用。这是类 X 中的一个方法,
projectDialog = new FilePathDialog();
projectDialog.setVisible(true);
projectDialog.addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {
System.out.println("Window closing");
try {
doWork();
} catch (Throwable t) {
t.printStackTrace();
}
}
public void windowClosed(WindowEvent e) {
System.out.println("Window closed");
try {
doWork();
} catch (Throwable t) {
t.printStackTrace();
}
}
});
当 JDialog 窗口关闭时,doWork 永远不会被调用。我想做的就是等待 JDialog 关闭,然后再继续该方法。我还尝试使用 SwingWorker 和 Runnable 但这没有帮助。
I have a class FilePathDialog which extends JDialog and that class is being called from some class X. Here is a method in class X
projectDialog = new FilePathDialog();
projectDialog.setVisible(true);
projectDialog.addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {
System.out.println("Window closing");
try {
doWork();
} catch (Throwable t) {
t.printStackTrace();
}
}
public void windowClosed(WindowEvent e) {
System.out.println("Window closed");
try {
doWork();
} catch (Throwable t) {
t.printStackTrace();
}
}
});
doWork never gets called when the JDialog window closes. All I'm trying to do is wait for the JDialog to close before it proceeds in the method. I also tried using SwingWorker and Runnable but that did not help.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
同样,关键是对话框是否为模态?
如果它是模态的,则不需要 WindowListener,因为您将知道对话框已被处理,因为代码将立即在对话框上调用
setVisible(true)
下面恢复。即,这应该有效:如果另一方面它是无模式的,那么 WindowListener 应该可以工作,并且您可能会在此处未显示的代码中遇到另一个问题,并且您需要发布 sscce 供我们分析、运行和修改。
编辑 gpeche
请查看这个 SSCCE,它显示了 3 种类型的默认关闭参数将触发窗口侦听器:
Again, the key is is the dialog modal or not?
If it's modal, then there's no need for a WindowListener as you will know that the dialog has been dealt with since code will resume immediately below your call to
setVisible(true)
on the dialog. i.e., this should work:If on the other hand it's mode-less, then a WindowListener should work, and you've likely got another problem in code not shown here, and you'll want to post an sscce for us to analyze, run, and modify.
Edit for gpeche
Please check out this SSCCE that shows that the 3 types of default closing parameters will trigger the window listener:
WindowListener.windowClosed()
:当由于在窗口上调用dispose而关闭窗口时调用。
对于
JDialog.setDefaultCloseOperation()
:设置当用户在此对话框上发起“关闭”时默认发生的操作。可能的选择有:
该值默认设置为 HIDE_ON_CLOSE。
这表明您应该在实例化
FilePathDialog
后调用projectDialog.setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
。Javadoc for
WindowListener.windowClosed()
:Invoked when a window has been closed as the result of calling dispose on the window.
And for
JDialog.setDefaultCloseOperation()
:Sets the operation which will happen by default when the user initiates a "close" on this dialog. The possible choices are:
The value is set to HIDE_ON_CLOSE by default.
That suggests that you should call
projectDialog.setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
after instantiatingFilePathDialog
.回答您的问题
将 WindowListener 添加到您的 JDialog (JDialog.DO_NOTHING_ON_CLOSE) ,在 windowClosing 事件尝试运行您的代码,如果成功结束,则调用
dispose()
或setVisible(false)
我不同意您的想法,另一种解决方法
仅使用
WindowListener
创建一个JDialog (JDialog.DO_NOTHING_ON_CLOSE)
,并将其重新用于另一个Action
,最终操作将始终为 < code>setVisible(false),如果您的代码成功结束,则从JDialog
中删除子级,您的JDialog
已准备好进行另一项工作answer to your question
add WindowListener to your JDialog (JDialog.DO_NOTHING_ON_CLOSE) , on windowClosing event to try to run your code, if ends with success then call
dispose()
, orsetVisible(false)
I don't agreed with your idea, another workaround
create only one
JDialog (JDialog.DO_NOTHING_ON_CLOSE)
withWindowListener
, and re-use that for anotherAction
, final action will be alwayssetVisible(false)
, if your code ends with success, then remove the child(s) fromJDialog
, yourJDialog
is prepared for another job