Using swingworker to update a JProgressBar during download

我怕爱的太早我们不能终老 提交于 2019-12-20 03:40:50

问题


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();
        }
    });
}

}


回答1:


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.



来源:https://stackoverflow.com/questions/11594139/using-swingworker-to-update-a-jprogressbar-during-download

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!