Kyte
Kyte

Reputation: 834

Using swingworker to update a JProgressBar during download

QUESTION SOLVED!!!!!! Many thanks to trashgod and HoverCraftFullOfEels! I finally got the concept by using the below example and altering it slightly. The alteration allows scaling the progress bar (by default is 100 units). Again, THANK YOU for your patience and willingness to work through this. Means a lot guys, ~Kyte

ps - +1's all 'round :)

/** @see http://stackoverflow.com/questions/4637215 */
public class Threading_01 extends JFrame {

private static final String s = "0.00";
private JProgressBar progressBar = new JProgressBar(0, 100);
private JLabel label = new JLabel(s, JLabel.CENTER);

public Threading_01() {
    this.setLayout(new GridLayout(0, 1));
    this.setTitle("√2");
    this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    this.add(progressBar);
    this.add(label);
    this.setSize(161, 100);
    this.setLocationRelativeTo(null);
    this.setVisible(true);
}

public void runCalc() {
//    progressBar.setIndeterminate(true);
    TwoWorker task = new TwoWorker();
    task.addPropertyChangeListener(new PropertyChangeListener() {

        @Override
        public void propertyChange(PropertyChangeEvent e) {
            if ("progress".equals(e.getPropertyName())) {
                progressBar.setIndeterminate(false);
                progressBar.setValue((Integer) e.getNewValue());
                System.out.println("**: " + e.getNewValue());
            }
        }
    });
    task.execute();
}

private class TwoWorker extends SwingWorker<Double, Double> {

    private static final int N = 734;
    private final DecimalFormat df = new DecimalFormat(s);

    @Override
    protected Double doInBackground() throws Exception {
        int cntr = 1; //
        double d1;
        double d2;
        double d3;
        for (int i = 0; i <=N; i++) {   
            d1 = N;
            d2 = i;
            d3 = d2/d1;
            d3 = d3 * 100;
            System.out.println(i + "++ " + d3);
            if(d3 >= cntr){
                System.out.println(i + "UPDATE");
                cntr++;
                setProgress(cntr);
                publish(Double.valueOf(cntr));
                Thread.sleep(250); // simulate latency
            }
        }
        return Double.valueOf(0);
    }

    @Override
    protected void process(List<Double> chunks) {
        for (double d : chunks) {
            label.setText(df.format(d));
            System.out.println(": " + d);
        }
    }
}

public static void main(String[] args) {
    EventQueue.invokeLater(new Runnable() {

        @Override
        public void run() {
            Threading_01 t = new Threading_01();
            t.runCalc();
        }
    });
}

}

Upvotes: 2

Views: 5125

Answers (1)

trashgod
trashgod

Reputation: 205875

Updating a progress bar is usually handled in a PropertyChangeListener, as shown here. See also this related example. Invoking setIndeterminate(true) is a reasonable initial setting if your worker can later offer more definitive progress.

Upvotes: 5

Related Questions