Reputation: 21
I want to use a progress monitor to show progress during the start of a real complex dialog!!!
The dialog creation is done within the event dispatcher thread. It can take up to 10 seconds, depending on the client system.
My desire is then to see a progress monitor in the foreground which shows status of each current loading part. Meanwhile the Dialog is building up in the background.
The problem is the progressmonitor always freezes until the dialog is started. When the dialog starting is finished the progressmonitor reacts and shows the current state.
I tried a lot of things read in forums, but without two different threads it doesn't work. Here is the example that let both the progress monitor and the dialog creation running in the EDT.
final CVProgressMonitorDialog progressMonitor = new CVProgressMonitorDialog();
progressMonitor.startPollingThread();
SwingUtilities.invokeLater(new Runnable() {
@Override
public void run() {
try {
startComplexDialog();
}
finally {
close(progressMonitor);
}
}
});
The CVProgressMonitorDialog is mainly the ProgressMonitor shipped with swing.
The solution is to let the creation of the dialog run in an other thread.
final CVProgressMonitorDialog progressMonitor = new CVProgressMonitorDialog();
progressMonitor.startPollingThread();
new Thread() {
@Override
public void run() {
try {
startComplexDialog();
}
finally {
close(progressMonitor);
}
}
}.start();
Do using the second solution causes potential troubles since swing is not threadsafe?
Is there a general solution to monitoring task within the EDT?
Upvotes: 2
Views: 538
Reputation: 1440
As stated in another answer, you shouldn't do lengthy operations in the EDT. But if you are in a hurry, I've managed a similar situation using a modal JDialog. When a modal dialog is made visible, it will block the thread making the setVisible(true) call and get a dispatch thread of it's own. It can be updated independently from the EDT. Do your progress bar in one of those and it should be fine.
Upvotes: 0
Reputation: 285403
You state:
The dialog creation is done within the event dispatcher thread. It can take up to 10 seconds, depending on the client system.
And there is your problem. You need to run any non-Swing long-running code in a background thread such as that supplied by a SwingWorker, but taking care to be sure that any code that changes the state of a visible Swing component be done on the event thread.
My desire is then to see a progress monitor in the foreground which shows status of each current loading part. Meanwhile the Dialog is building up in the background.
Again, do your work in a SwingWorker-derived background thread, updating the SwingWorker's progress property as the creation progresses, and using a PropertyChangeListener added to your SwingWorker to monitor and to display changes to the progress.
Upvotes: 1