views:

556

answers:

3

How can I make a modal JDialog without buttons appear for the duration it takes a Runnable instance to complete and have that instance update a progress bar/message on that Dialog.

Clearly spaghetti code might be this working, but I'm looking for a clean design if one exists.

Thanks.

A: 

Use a monitor class whit a global instance and which your code keeps up to date (I'm starting, I'm working, I'm at xxx%, I'm done).

The monitor can then decide to show the dialog and keep it current. Later, you can simply replace the dialog with a progress bar in the status bar, for example. Use an interface for the monitor (methods: start(), update(), end(), error(), isAborted()) so you can replace it with something else, too.

You can extend the monitor to wait for 500ms after start() if there is an end() and not show the dialog in this case, etc.

This is how Eclipse does it and it works well.

Aaron Digulla
+3  A: 

You might want to look into ProgressMonitor. It automatically pops up a dialog with a progress bar if the operation is taking a long time; see How to Use Progress Monitors.

Michael Myers
Except for the cancel button? :P
Michael Myers
Yes, it's not ideal, but it does answer the question exactly.
Allain Lalonde
A: 

Look at the project described at the following URL: http://digilander.libero.it/carlmila/compmon/main.html

It is a free Java library alternative to the Swing ProgressMonitor.

carlo.milanesi