Downloading file/files in Java. Multithreading, this works?

后端 未结 1 503
情深已故
情深已故 2021-01-23 11:42

First, everyone needs to know i\'m relatively new to Java coding. To be more precise i\'m completely new to Object Oriented Programming.


To the question.

相关标签:
1条回答
  • 2021-01-23 12:22

    Suggestions:

    • Use a SwingWorker to do your background thread work.
    • Inside your SwingWorker, set its progress "bound" property via setProgress(int progress). The value should be between 1 and 100.
    • Don't have your SwingWorker/file downloader hold the JProgressBar or any Swing components.
    • Add a PropertyChangeListener to your SwingWorker and monitor changes in the progress property.
    • Never make your Swing fields (or most and and all fields) public. Limit access, and instead change object state via methods.
    • Read the tutorial Concurrency in Swing for the necessary details.

    For example, the code below is a gross simplification and downloads no files, but should give you the idea:

    import java.awt.*;
    import java.beans.PropertyChangeEvent;
    import java.beans.PropertyChangeListener;
    import java.util.Random;
    
    import javax.swing.*;
    
    public class Initial {
    
       static AtomFrame atomLauncher;
    
       public static void main(String[] args) {
    
          atomLauncher = new AtomFrame();
          atomLauncher.start();
    
          System.out.println(Integer.MAX_VALUE);
    
          final Download theDownload = new Download();
          theDownload.addPropertyChangeListener(new PropertyChangeListener() {
    
             @Override
             public void propertyChange(PropertyChangeEvent pcEvt) {
                if ("progress".equals(pcEvt.getPropertyName())) {
                   int progress = theDownload.getProgress();
                   atomLauncher.setProgress(progress);
                }
             }
          });
    
          theDownload.execute();
       }
    
    }
    
    class AtomFrame extends JFrame {
    
       // ********* should be private!
       private JProgressBar progressBar;
    
       private static final long serialVersionUID = 4010489530693307355L;
    
       public static void main(String[] args) {
          AtomFrame testFrame = new AtomFrame();
          testFrame.start();
       }
    
       public void setProgress(int progress) {
          progressBar.setValue(progress);
       }
    
       public AtomFrame() {
          initializeComponents();
       }
    
       public void initializeComponents() {
          this.setSize(400, 400);
          this.setLocationRelativeTo(null);
          this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
          this.setTitle("Atom Launcher");
          this.setLayout(new FlowLayout(FlowLayout.CENTER, 5, 5));
    
          progressBar = new JProgressBar();
          this.add(progressBar);
    
          // this.pack();
       }
    
       public void start() {
          this.setVisible(true);
       }
    
       public void close() {
          this.dispose();
       }
    }
    
    class Download extends SwingWorker<Void, Void> {
       private static final long SLEEP_TIME = 300;
       private Random random = new Random();
    
       @Override
       protected Void doInBackground() throws Exception {
          int myProgress = 0;
          while (myProgress < 100) {
             myProgress += random.nextInt(10);
             setProgress(myProgress);
             try {
                Thread.sleep(SLEEP_TIME);
             } catch (InterruptedException e) {}
          }
          return null;
       }
    }
    
    0 讨论(0)
提交回复
热议问题