How create progress bar while file transferring

前端 未结 2 806
青春惊慌失措
青春惊慌失措 2020-12-07 06:38
import java.awt.Component;
import java.io.BufferedInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.I         


        
2条回答
  •  被撕碎了的回忆
    2020-12-07 07:06

    I can think of two ways.

    Swing Worker

    Start by wrapping you copy code into a SwingWorker, using the setProgress method to update the progress and a property change listener to monitor changes to the progress property.

    When the progress property changes, you would then update the UI.

    This solution will require you to supply you own UI

    Progress Monitor

    Use a ProgressMonitorInputStream, which comes with it's own UI.

    InputStream in = new BufferedInputStream(
        new ProgressMonitorInputStream(
            parentComponent,
            "Reading " + fileName,
            new FileInputStream(fileName)));
    

    (Example stolen from Java Docs)

提交回复
热议问题