Solved

Wait for task doInBackground() to finish

Posted on 2011-03-19
14
1,274 Views
Last Modified: 2012-05-11
How do I wait for ProgressTask to finish before procedding?
(see ???)

import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JProgressBar;
import javax.swing.SwingWorker;
import javax.swing.UIManager;

public class ProgressPanel extends JPanel {

	private JProgressBar jProgressBar;

	/**
	 * constructor
	 * 
	 */
	public ProgressPanel() {
		
		this.setLayout(new BorderLayout());
		jProgressBar = new JProgressBar();
		JButton button = new JButton("Go");
		this.add(button, BorderLayout.LINE_START);
		this.add(jProgressBar, BorderLayout.LINE_END);

		button.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				jProgressBar.setIndeterminate(true);
				new ProgressTask().execute(); // doInBackground()
				
				// how to wait for ProgressTask to finish???
				System.out.println ("Do other stuff, but wait for task to finish");
			}
		});
	}


	/**
	 * 
	 */
	private void doStuff() {
	    // do something	
		System.out.println("ProgressPanel.doStuff()");
	}
	
	/**
	 *
	 * @param args
	 */
	public static void main(String[] args) {
		try {
			UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
		} catch (final Exception e) {
			e.printStackTrace();
		}

		try {
			JFrame frame = new JFrame("This is a test");
			frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
			ProgressPanel progressPanel = new ProgressPanel();
			frame.add(progressPanel);
			frame.setSize(300, 100);
			frame.setVisible(true);
		} catch (final Exception e) {
			e.printStackTrace();
		}
	}
	
	/**
	 * constructor
	 */
	private class ProgressTask extends SwingWorker<Void, Void> {
		
		public Void doInBackground() {
			
			doStuff();				

			return null;
		}

		public void done() {
			jProgressBar.setIndeterminate(false);
		}
	}
}

Open in new window

0
Comment
Question by:allelopath
  • 7
  • 4
  • 3
14 Comments
 
LVL 47

Expert Comment

by:for_yan
ID: 35172583
I guess you can have some instance boolean variable in your
ProgresPanel class and after doStuff() executes in ProgressTask in doItInBackground()
you can toggle this variable - and then you can check it
at any point you want to do something dependent on
the completeion of that process.
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 35172643
done() will be called when it's done

>>before procedding?

If you mean you don't want the user to do anything until it's finished, disable the gui beforehand and re-enable in done()
0
 
LVL 1

Author Comment

by:allelopath
ID: 35172684
I tried the boolean, but doesn't work, maybe I am doing something wrong:

import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JProgressBar;
import javax.swing.SwingWorker;
import javax.swing.UIManager;

public class ProgressPanel extends JPanel {

	private JProgressBar jProgressBar;

	private boolean done;
	
	/**
	 * constructor
	 * 
	 */
	public ProgressPanel() {
		
		done = false;
		
		this.setLayout(new BorderLayout());
		jProgressBar = new JProgressBar();
		JButton button = new JButton("Go");
		this.add(button, BorderLayout.LINE_START);
		this.add(jProgressBar, BorderLayout.LINE_END);

		button.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				jProgressBar.setIndeterminate(true);
				new ProgressTask().execute(); // doInBackground()
				
				while (! done) {
					System.out.println ("done =" + done);
				} // wait
				
				// how to wait for ProgressTask to finish???
				System.out.println ("Do other stuff, but wait for task to finish");
			}
		});
	}


	/**
	 * 
	 */
	private void doStuff() {
	    // do something	
		System.out.println("ProgressPanel.doStuff()");
	}
	
	/**
	 *
	 * @param args
	 */
	public static void main(String[] args) {
		try {
			UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
		} catch (final Exception e) {
			e.printStackTrace();
		}

		try {
			JFrame frame = new JFrame("This is a test");
			frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
			ProgressPanel progressPanel = new ProgressPanel();
			frame.add(progressPanel);
			frame.setSize(300, 100);
			frame.setVisible(true);
		} catch (final Exception e) {
			e.printStackTrace();
		}
	}
	
	/**
	 * constructor
	 */
	private class ProgressTask extends SwingWorker<Void, Void> {
		
		public Void doInBackground() {
			
			System.out.println("ProgressTask.doInBackground(): ");
			
			doStuff();				

			return null;
		}

		public void done() {
			System.out.println("ProgressTask.done() ");
			done = true;
			
			jProgressBar.setIndeterminate(false);
		}
	}
}

Open in new window

0
Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
LVL 86

Expert Comment

by:CEHJ
ID: 35172694
That's not going to help you - you'd require another thread or at least a loop to check that. Much better to take a similar approach to the one i mentioned
0
 
LVL 47

Expert Comment

by:for_yan
ID: 35172706
But that's what the author did - placed it in the done() method - it should work

In what sense it is not working please explain
0
 
LVL 1

Author Comment

by:allelopath
ID: 35173017
It never gets past the while(!done) loop, ie done is never set to true.
It is set to true in the done() method, but that method is never reached.
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 35173027
allelopath - what do you (not) want the user to do?
0
 
LVL 47

Expert Comment

by:for_yan
ID: 35173045
Still try to set this varoiable immediatly after doStouff() in the same method; perhaps it will work
0
 
LVL 86

Accepted Solution

by:
CEHJ earned 500 total points
ID: 35173053
Have a look at the following and watch the button after you click it:
import java.awt.BorderLayout;
import java.awt.Container;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowEvent;
import java.awt.event.WindowListener;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JProgressBar;
import javax.swing.SwingWorker;
import javax.swing.UIManager;


public class ProgressPanel extends JPanel {
    JProgressBar jProgressBar;
    JButton bnGo;

    public ProgressPanel() {
	this.setLayout(new BorderLayout());
	jProgressBar = new JProgressBar();
	bnGo = new JButton("Go");
	this.add(bnGo, BorderLayout.LINE_START);
	this.add(jProgressBar, BorderLayout.LINE_END);

	bnGo.addActionListener(new ActionListener() {
	    public void actionPerformed(ActionEvent e) {
		jProgressBar.setIndeterminate(true);
		bnGo.setEnabled(false);
		new ProgressTask().execute();
	    }
	});
    }

    /**
     *
     * @param indeterminate
     */
    public void setProgressIndeterminate(boolean indeterminate) {
	if (indeterminate) {
	    System.out.println("ProgressPanel.setProgressIndeterminate(): true");
	    jProgressBar.setIndeterminate(true);
	} else {
	    System.out.println(
		    "ProgressPanel.setProgressIndeterminate(): false");
	    jProgressBar.setIndeterminate(false);
	    jProgressBar.setValue(0);
	}

	this.updateUI();
    }

    /**
     *
     * @param args
     */
    public static void main(String[] args) {
	try {
	    UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
	} catch (final Exception e) {
	    e.printStackTrace();
	}

	try {
	    JFrame frame = new JFrame("This is a test");
	    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
	    ProgressPanel progressPanel = new ProgressPanel();
	    frame.add(progressPanel);
	    frame.setSize(300, 100);
	    frame.setVisible(true);
	} catch (final Exception e) {
	    e.printStackTrace();
	}
    }

    /**
     * constructor
     */
    private class ProgressTask extends SwingWorker<Void, Void> {
	public Void doInBackground() {
	    try {
		System.out.println("Sleeping for two secs...");
		Thread.sleep(2000);
	    } catch (InterruptedException e) {
		/* ignore */
	    }

	    return null;
	}

	public void done() {
	    jProgressBar.setIndeterminate(false);
	    bnGo.setEnabled(true);
	}
    }
}

Open in new window

0
 
LVL 1

Author Comment

by:allelopath
ID: 35173134
So when you said disable the gui, you meant disable the button. Makes more sense now.
Your code is a step in the right direction, but my original question remains.
If I add the done boolean, setting it to true in the done() method, it never gets past the while loop because done() is not called.

Keep in my that this is a test program, in the 'real' application, I want to wait for the task to finish before moving on to do other stuff (after the while loop)

0
 
LVL 86

Expert Comment

by:CEHJ
ID: 35173152
>> in the 'real' application, I want to wait for the task to finish before moving on to do other stuff (after the while loop)

All you need do is to disable whatever part(s) of the gui would allow the user to move on

0
 
LVL 86

Expert Comment

by:CEHJ
ID: 35173179
The theory is not really important, as the approach is clear (see above) but if you want it, what you're doing is effectively blocking the EDT so that done can never be called on it
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 35173370
:)
0
 
LVL 47

Expert Comment

by:for_yan
ID: 35173515
@alleopath
So can you explain me if it indeed executes done() method after it finishes doInBackground()?
0

Featured Post

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
configure dependency in POM for new database 3 34
Unhandled exception type Exception 18 43
ejb example issues 3 28
Which non-HTML GUI front end to use with Java? 3 24
Java contains several comparison operators (e.g., <, <=, >, >=, ==, !=) that allow you to compare primitive values. However, these operators cannot be used to compare the contents of objects. Interface Comparable is used to allow objects of a cl…
Introduction This article is the first of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article explains our test automation goals. Then rationale is given for the tools we use to a…
Viewers will learn about the different types of variables in Java and how to declare them. Decide the type of variable desired: Put the keyword corresponding to the type of variable in front of the variable name: Use the equal sign to assign a v…
Viewers will learn about if statements in Java and their use The if statement: The condition required to create an if statement: Variations of if statements: An example using if statements:

820 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question