Search code examples
javaswingswingworkerjprogressbar

JProgressBar while data loading in swing


I have a server call from the UI. It has response time is little high. So I was thinking to display a progress bar during data loading from the server. I have tried the following code using this approach to show the progress bar. Some where I am doing wrong I am not seeing the progress bar when I call the calculateResult() method on button click. I no need to display any percentage on the progress bar. It just needs to show that data is loading.

// The following code I have tried.

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.lang.reflect.InvocationTargetException;

import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JProgressBar;
import javax.swing.SwingUtilities;
import javax.swing.SwingWorker;

 public class MyProgressBarTest extends JFrame {
private static final long serialVersionUID = 1L;

private static JProgressBar progressBar;

public static void main(String[] args) {
    MyProgressBarTest obj = new MyProgressBarTest();
    obj.createGUI();
}

public void createGUI() {
    JPanel panel = new JPanel();
    JButton button = new JButton("Progress");

    progressBar = new JProgressBar();

    button.addActionListener(new ActionListener() {
        @Override
        public void actionPerformed(ActionEvent arg0) {

            MyCustomProgressBarDialog progressBarObj = new    MyCustomProgressBarDialog(progressBar);
            progressBarObj.createProgressUI();

            MyActionPerformer actionObj = new MyActionPerformer(progressBar);
            actionObj.execute();

            progressBarObj.setVisible(false);
        }
    });

//      panel.add(progressBar);
    panel.add(button);
    add(panel);

    setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );
    setLocationRelativeTo(null);
//      pack();
    setSize(200, 300);
    setVisible(true);
}
}

class MyActionPerformer extends SwingWorker<String, Object> {

JProgressBar fProgressBar;

public MyActionPerformer(JProgressBar progressBar) {
    this.fProgressBar = progressBar;
    this.fProgressBar.setVisible(true);
    this.fProgressBar.setIndeterminate(true);
}

protected String doInBackground() throws Exception {

    calculateResult();
    return "Finished";
}

protected void done() {
    fProgressBar.setVisible(false);
}

public void calculateResult() {
    for (int i = 0; i < 500000; i++) {
        System.out.println("Progress Bar: " + i);
    }
}
}

class MyCustomProgressBarDialog extends JDialog {
private static JProgressBar progressBar;

public MyCustomProgressBarDialog(JProgressBar progressBar) {
    this.progressBar = progressBar;
}

public void createProgressUI() {
    add(progressBar);
    setLocationRelativeTo(null);
    setSize(50, 20);
    setVisible(true);
}
}

Solution

  • The reason your progress bar disappears immediately is your ActionListener

    button.addActionListener(new ActionListener() {
      @Override
      public void actionPerformed(ActionEvent arg0) {
       MyCustomProgressBarDialog progressBarObj = new    MyCustomProgressBarDialog(progressBar);
       progressBarObj.createProgressUI();
    
       MyActionPerformer actionObj = new MyActionPerformer(progressBar);
       actionObj.execute();
    
       progressBarObj.setVisible(false);
      }
    });
    

    The actionObj.execute(); method is not blocking (good thing or it would be useless) meaning that immediately after you start the SwingWorker with that call you will execute the progressBarObj.setVisible(false); statement.

    This causes the progress bar dialog to disappear.

    I can think of 2 solutions for this

    • Pass the dialog to the SwingWorker as well and call setVisible( false ) on the dialog in the done method of the SwingWorker
    • A SwingWorker fires PropertyChangeEvents which allow you to determine how far it progressed. You can use such a listener to hide the dialog when the calculations are finished