We help IT Professionals succeed at work.

Display Progress Bar when already in event thread

allelopath asked
The code below works fine. The problem I've having is when I try to integrate this code in my existing application. The problem is that this code needs to run inside code that is already part of the  event thread. If I run with invokeLater(), then the work is done (in this test case, the 3 second delay) and then the progress bar is displayed and then taken down. I can't use invokeAndWait() because, as stated, this is already in the event thread in my application. If I done use invokeLater(), then the progress bar is displayed, but the code gets stuck there, it does not advance to the work to be done.

So the question is ... while already in the event thread, how to I display the progress bar, do the work, then take down the progress bar?

package callablewithswing;

import java.awt.Dimension;
import java.util.HashSet;
import java.util.Set;
import java.util.concurrent.Callable;
import java.util.concurrent.CancellationException;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.Future;

import javax.swing.JDialog;
import javax.swing.JProgressBar;
import javax.swing.SwingUtilities;

public class CallableDemo {
	 * constructor
	public CallableDemo() {

		final GenericProgressDialog genericProgressDialog = new GenericProgressDialog("Test Callable");
		// ensure progress bar updates happen on the event dispatch thread
		SwingUtilities.invokeLater(new Runnable() {
			public void run() {
		Set<Callable<Integer>> taskSet = new HashSet<Callable<Integer>>();

		// add a task to the set
		taskSet.add(new Callable<Integer>() {
			public Integer call() throws Exception {
				// sleep 3 seconds total, 1 second at a time
				for (int s = 0; s < 3; s++) { 
					System.out.println (s+1);
				return 2;

		ExecutorService executor = Executors.newSingleThreadExecutor();

		for (Callable<Integer> task : taskSet) {
			Future<Integer> future = executor.submit(task);
			// wait for result, then continue
			try {
				Integer result = future.get();
				System.out.println("Finished Task.  Result: " + result);
			catch (InterruptedException ie) {
				// thread was interrupted while waiting
				System.err.println("Thread interrupted, exiting.");
			catch (CancellationException ce) {
				// task was cancelled
				System.out.println("Task Cancelled");
			catch (ExecutionException ee) {
				// exception thrown in Callable<>.call()
				System.err.println("Exception thrown during task execution: ");
				// ee.getCause() is the exception thrown in the call() method

	 * a dialog box with progress bar
	public class GenericProgressDialog extends JDialog {

		public GenericProgressDialog(String title) {
			final JProgressBar jProgressBar = new JProgressBar();

			final int width = 300;
			final int height = 60;

			this.setPreferredSize(new Dimension(width, height));
			this.setLocation( width/2, height/2 );
	 * @param s
	public static void main(String[] s) {

		new CallableDemo();


Open in new window

Watch Question

Top Expert 2016
I can't help but think that your recent path to arrive at this point has taken you to the wrong destination. What is your goal? To make a progress bar work correctly in the normal way? If so, you should normally be using a SwingWorker
Awarded 2011
Awarded 2011
As we anyway cannot access your code and understand what is the problem, perhaps you could compare your situation to
some standard case which we already know is working and try to formulate in what way your situation is different.
For instance you may want to look to this  example, which works fine for me:


Maybe if you look at it and formulate in which way your case is different - maybe it might help.
Top Expert 2016