Skip to Main Content

Java SE (Java Platform, Standard Edition)

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

Waiting for a thread to finish

843806Dec 11 2008 — edited Dec 11 2008
I've been researching this issue for a couple of days, but I dont think I know enough about threading to ask the right question in a search engine. Sorry if this is a basic blunder....I dont write swing often and it shows.

I have a swing app that includes a long task so I have implemented a progress bar. I need to wait until the progress bar task completes before continuing to the rest of the method. I've tried:
1. Putting the progress bar within a while loop (while task not complete...) but the progress bar dialog does not render fully. I've even added repaint, but still the dialog looks blank.
2. SwingUtilities.invokeLater, but it doesnt wait until the progress bar is finished
3. SwingUtilities.invokeAndWait, though after all the reading I did about deadlock conditions I didnt like the idea. However I was desperate so I tried it and got an error:"Cannot call invokeAndWait from the event dispatcher thread".
4. Putting the code that needs to occur after the progress bar in an "invokeLater" thread
5. I've also used the ProgressBarDemo from the java.sun example with the swingworker hoping the worker thread would handle the issue.

A much smaller version of the code is below:
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.border.Border;

public class ProblemCode extends JFrame
{
	private JFrame jFrame = null;
	private JPanel jMain = null;
	private JButton startButton = null;
	
	public ProblemCode()
	{
		initialize();
	}
	public void initialize()
	{
		//Setup the main application window size
		jFrame = new JFrame();
		jFrame.setContentPane(getJMainPanel());
		jFrame.pack();
		jFrame.addWindowListener(new java.awt.event.WindowAdapter() 
		{ 
			public void windowClosing(java.awt.event.WindowEvent e) 
			{
				dispose();
			}
		});
		
		SwingUtilities.invokeLater(new Runnable()
		{
            public void run()
            {
                jFrame.setVisible(true);
            }
		});
	}
	private JPanel getJMainPanel()
	{
		if(jMain == null)
		{
			jMain = new JPanel();
			jMain.add(getJStartButton());
		}
		return jMain;
	}
	private JButton getJStartButton()
	{
		if (startButton == null) 
		{
			startButton = new JButton();
			startButton.setText("Start");
			startButton.setVisible(true);
			startButton.addActionListener(new ActionListener() 
		    { 
		    	public void actionPerformed(ActionEvent e) 
		    	{
		    	    SwingUtilities.invokeLater(new Runnable()
			    	{
			    		public void run()
			    		{
					   		progBar();
			    		}
			    	});
		    		System.out.println("Do this after the progress bar completes");
		    		SwingUtilities.invokeLater(new Runnable()
		    		{
		    			public void run()
		    			{
				    		System.out.println("invokeLater doesnt work either....");
		    			}
		    		});
		        }
		     });					
		}
		return startButton;
	}

	private void progBar()
	{
        JFrame jProgFrame = new JFrame("JProgressBar Sample");
        jProgFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        Container content = jProgFrame.getContentPane();
        final JProgressBar progressBar = new JProgressBar();
        
        new Thread()
        {
            private int counter = 0;
            private final int val = (int)(Math.random()*10)+1;
            public void run()
            {
                for(int i = 0;i <= 100;i++)
                {
                    counter += val;
                    javax.swing.SwingUtilities.invokeLater( new Runnable()
                    {
                        public void run()
                        {
                            progressBar.setValue(counter);
                        }
                    });
                    try
                    {
                        Thread.sleep(500);
                    }
                    catch (InterruptedException e)
                    {
                        
                    }
                }
            }
        }.start();
        
        progressBar.setStringPainted(true);
        Border border = BorderFactory.createTitledBorder("Reading...");
        progressBar.setBorder(border);
        content.add(progressBar, BorderLayout.NORTH);
        jProgFrame.setSize(300, 100);
        jProgFrame.setVisible(true);
	}
	public static void main(String[] args)
	{
		SwingUtilities.invokeLater(new Runnable()
		{
			public void run()
			{
				new ProblemCode();
			}
		});
	}
}
Thank you for taking the time to review this.
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on Jan 8 2009
Added on Dec 11 2008
3 comments
177 views