Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Simulating a long-running operation

How could I simulate a long-running database operation? My requirement is to display a dialog box with a JProgressBar until the operation is completed.

like image 608
James P. Avatar asked Nov 26 '25 13:11

James P.


1 Answers

Use a background thread (such as one provided by a SwingWorker) to run a for loop going from 1 to 10 with a Thread.sleep inside of the loop. Then if it is a determinate mode JProgressBar, you can update its value by passing 10 * the loop index to the progress bar (taking care to do so on the Swing thread, the EDT, of course).

Edit:
@James Poulson: If you're using a SwingWorker object, you would use the publish/process methods and the done method to update on the EDT. If you're using your own background thread, you'd be sure to wrap any group of Swing calls in a Runnable and queue it on the EDT with invokeLater.

like image 186
Hovercraft Full Of Eels Avatar answered Nov 29 '25 01:11

Hovercraft Full Of Eels



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!