简体   繁体   English

在下载过程中使用swingworker更新JProgressBar

[英]Using swingworker to update a JProgressBar during download

QUESTION SOLVED!!!!!! 问题解决了!!!!!! Many thanks to trashgod and HoverCraftFullOfEels! 非常感谢rashgod和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). 更改允许缩放进度条(默认为100个单位)。 Again, THANK YOU for your patience and willingness to work through this. 再次感谢您的耐心和意愿。 Means a lot guys, ~Kyte 意思很多,〜Kyte

ps - +1's all 'round :) ps-+1全部为回合:)

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

} }

Updating a progress bar is usually handled in a PropertyChangeListener , as shown here . 更新进度条通常在处理PropertyChangeListener ,如图所示这里 See also this related example . 另请参阅此相关示例 Invoking setIndeterminate(true) is a reasonable initial setting if your worker can later offer more definitive progress. 如果您的工作人员以后可以提供更确定的进度,则调用setIndeterminate(true)是一个合理的初始设置。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM