How can I avoid, that a dialog is shown, when there already is one on the screen?
当屏幕上有一个对话框时,如何避免显示对话框?
Details: In my application many Timers are running. If a fatal error occurs, all the affected threads are going to show a JDialog (by swingx.JXErrorPane.showDialog()), which is not desired. Even if I cancel all the running Timers, still some Dialogs will appear at the same time. How can I achieve that there will only appear one dialog?
细节:在我的应用程序中,许多计时器正在运行。如果发生致命错误,所有受影响的线程将显示JDialog(通过swingx.JXErrorPane.showDialog()),这是不希望的。即使我取消所有正在运行的计时器,仍会有一些对话框同时出现。我怎样才能实现只出现一个对话框?
I tried to make the method which calls showDialog() synchronized, which results in my whole GUI being blocked. Usage of a flag didn't work either.
我试图使调用showDialog()的方法同步,这导致我的整个GUI被阻止。使用标志也不起作用。
2 个解决方案
#1
Turn the dialog into an observer and publish the error events to this new observer (see the Observer Design Pattern). When an event happens, the dialog should show itself (with the main frame as the parent). Display the recent errors in a table in the dialog. Add new errors to the bottom of that table.
将对话框转换为观察者并将错误事件发布到此新观察者(请参阅观察者设计模式)。当事件发生时,对话框应显示自身(主框架为父框架)。在对话框中的表中显示最近的错误。将新错误添加到该表的底部。
Another option is to display the errors in a table in the main frame. Put the table into a JSplitPane so users can minimize it.
另一种选择是在主框架中的表格中显示错误。将表放入JSplitPane,以便用户可以将其最小化。
#2
Yet another option would be to turn the dialog into a singleton:
另一个选择是将对话框变成单例:
- Give the dialog a private constructor.
- Create a "private static MyDialog instance;" - attribute in the dialog class.
- Create a "public static MyDialog getInstance() { ... }; - method to return the instance and first instantiate it if it is null.
给对话框一个私有构造函数。
创建“私有静态MyDialog实例;” - 对话框类中的属性。
创建一个“public static MyDialog getInstance(){...}; - 返回实例的方法,如果它为null则首先实例化它。
Where MyDialog should be the name of the dialog window's class.
其中MyDialog应该是对话窗口类的名称。
Then each time you need to show the dialog you simply refer to the singleton:
然后每次需要显示对话框时,只需参考单例:
MyDialog.getInstance().showDialog();
Where showDialog just makes the dialog window visible.
showDialog只是使对话框窗口可见。
#1
Turn the dialog into an observer and publish the error events to this new observer (see the Observer Design Pattern). When an event happens, the dialog should show itself (with the main frame as the parent). Display the recent errors in a table in the dialog. Add new errors to the bottom of that table.
将对话框转换为观察者并将错误事件发布到此新观察者(请参阅观察者设计模式)。当事件发生时,对话框应显示自身(主框架为父框架)。在对话框中的表中显示最近的错误。将新错误添加到该表的底部。
Another option is to display the errors in a table in the main frame. Put the table into a JSplitPane so users can minimize it.
另一种选择是在主框架中的表格中显示错误。将表放入JSplitPane,以便用户可以将其最小化。
#2
Yet another option would be to turn the dialog into a singleton:
另一个选择是将对话框变成单例:
- Give the dialog a private constructor.
- Create a "private static MyDialog instance;" - attribute in the dialog class.
- Create a "public static MyDialog getInstance() { ... }; - method to return the instance and first instantiate it if it is null.
给对话框一个私有构造函数。
创建“私有静态MyDialog实例;” - 对话框类中的属性。
创建一个“public static MyDialog getInstance(){...}; - 返回实例的方法,如果它为null则首先实例化它。
Where MyDialog should be the name of the dialog window's class.
其中MyDialog应该是对话窗口类的名称。
Then each time you need to show the dialog you simply refer to the singleton:
然后每次需要显示对话框时,只需参考单例:
MyDialog.getInstance().showDialog();
Where showDialog just makes the dialog window visible.
showDialog只是使对话框窗口可见。