Download a file while also updating a JProgressBar

前端 未结 2 1577
北恋
北恋 2021-02-10 15:35

I have tried tones of different methods to get this to work but they either don\'t work with a progress bar or don\'t work the way I would like it to.

I have already cre

2条回答
  •  不要未来只要你来
    2021-02-10 16:11

    downloading the file in a new thread works fine:

    import java.awt.BorderLayout;
    import java.awt.Container;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    import java.util.concurrent.TimeUnit;
    import java.util.concurrent.atomic.AtomicBoolean;
    import java.util.concurrent.locks.LockSupport;
    
    import javax.swing.BorderFactory;
    import javax.swing.JButton;
    import javax.swing.JFrame;
    import javax.swing.JProgressBar;
    
    /**
     * 
     * @author adyliu(imxylz@gmail.com)
     * @since 2012-12-28
     */
    public class JProgressBarDemo {
    
        public static void main(String[] args) {
            final JProgressBar pbFile = new JProgressBar();
            pbFile.setValue(0);
            pbFile.setMaximum(100);
            pbFile.setStringPainted(true);
            pbFile.setBorder(BorderFactory.createTitledBorder("Download file"));
    
            JFrame theFrame = new JFrame("ProgressBar Demo");
            theFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    
            Container contentPane = theFrame.getContentPane();
            contentPane.add(pbFile, BorderLayout.SOUTH);
            final JButton btnDownload = new JButton("Download");
            contentPane.add(btnDownload);
            final AtomicBoolean running = new AtomicBoolean(false);
            btnDownload.addActionListener(new ActionListener() {
                public void actionPerformed(ActionEvent e) {
                    running.set(!running.get());
                    btnDownload.setText(running.get() ? "Pause" : "Continue");
                    if (running.get()) {
                        new Thread() {
                            public void run() {
                                //download file in a thread
                                int v = 0;
                                while (running.get() && v < pbFile.getMaximum()) {
                                    pbFile.setValue(++v);
                                    LockSupport.parkNanos(TimeUnit.MILLISECONDS.toNanos(200));
                                }
                            }
                        }.start();
                    }
                }
            });
            theFrame.setSize(300, 150);
            theFrame.setLocationRelativeTo(null);
            theFrame.setVisible(true);
        }
    
    }
    

提交回复
热议问题