Question

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?

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.

Was it helpful?

Solution

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.

OTHER TIPS

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.

Where MyDialog should be the name of the dialog window's class.

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.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top