Display Progress Bar when already in event thread

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() {
				genericProgressDialog.setVisible(true);
			}
		});
		
		Set<Callable<Integer>> taskSet = new HashSet<Callable<Integer>>();

		// add a task to the set
		taskSet.add(new Callable<Integer>() {
			@Override
			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);
					Thread.sleep(1000);
				}
				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.");
				ie.printStackTrace(System.err);
				break;
			} 
			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
				ee.getCause().printStackTrace(System.err);
			}
		}

		executor.shutdown();
		
		genericProgressDialog.setVisible(false);
		genericProgressDialog.dispose();
		
	}
	
	/**
	 * a dialog box with progress bar
	 *
	 */
	public class GenericProgressDialog extends JDialog {

		public GenericProgressDialog(String title) {
	        
			final JProgressBar jProgressBar = new JProgressBar();
			jProgressBar.setIndeterminate(true);
			
			this.setTitle(title);
			this.setModal(true);

			this.setDefaultCloseOperation(JDialog.DO_NOTHING_ON_CLOSE);
			
			final int width = 300;
			final int height = 60;

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

		new CallableDemo();

	}
}

Open in new window

LVL 1
allelopathAsked:
Who is Participating?
 
CEHJCommented:
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
0
 
for_yanCommented:
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:

http://www.java2s.com/Tutorial/Java/0240__Swing/SwingWorkerandProgressBar.htm

Maybe if you look at it and formulate in which way your case is different - maybe it might help.
0
 
CEHJCommented:
:)
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.