Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

Wait for task doInBackground() to finish

Posted on 2011-03-19
14
Medium Priority
?
1,339 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
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 2000 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

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Introduction Java can be integrated with native programs using an interface called JNI(Java Native Interface). Native programs are programs which can directly run on the processor. JNI is simply a naming and calling convention so that the JVM (Java…
Java Flight Recorder and Java Mission Control together create a complete tool chain to continuously collect low level and detailed runtime information enabling after-the-fact incident analysis. Java Flight Recorder is a profiling and event collectio…
Viewers will learn about arithmetic and Boolean expressions in Java and the logical operators used to create Boolean expressions. We will cover the symbols used for arithmetic expressions and define each logical operator and how to use them in Boole…
Viewers will learn about the regular for loop in Java and how to use it. Definition: Break the for loop down into 3 parts: Syntax when using for loops: Example using a for loop:
Suggested Courses

618 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