Allain Lalonde
Allain Lalonde

Reputation: 93448

Progress Dialog in Swing

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 work, but I'm looking for a clean design if one exists.

Upvotes: 4

Views: 22106

Answers (3)

Michael Myers
Michael Myers

Reputation: 192055

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.

Upvotes: 8

carlo.milanesi
carlo.milanesi

Reputation: 209

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.

Upvotes: 2

Aaron Digulla
Aaron Digulla

Reputation: 328830

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.

Upvotes: 1

Related Questions