I simply want to have an indeterminate JProgressBar animate in the bottom left corner of my frame when a long download is being done.
I've looked through many tutorials, none of which are clear to me. I simply want to have it animate while the file is being downloaded in the background. Each way I've tried this, it doesn't animate the progress bar until after the download is done.
I need help knowing where to place my download() call.
class MyFunClass extends JFrame {
JProgressBar progressBar = new JProgressBar();
public void buttonClicked() {
progressBar.setVisible(true);
progressBar.setIndeterminate(true);
SwingUtilities.invokeLater(new Runnable() {
public void run() {
progressBar.setIndeterminate(true);
progressBar.setVisible(true);
// Do I do my download() in here??
}});
// Do download() here???
progressBar.setVisible(false);
}
}
Thanks in advance!
Solution ========
Edit: For those who have a similar issue to me in the future, this is the basic solution for a basic problem. This isn't my code verbatim, but a general sketch. Inside buttonClicked()
:
public void buttonClicked() {
class MyWorker extends SwingWorker(String, Object) {
protected String doInBackground() {
progressBar.setVisible(true); <-- should be on the EDT
progressBar.setIndeterminate(true);<-- should be on the EDT
// Do my downloading code
return "Done."
}
protected void done() {
progressBar.setVisible(false)
}
}
new MyWorker().execute();
}