How do I use JProgressBar to display file copy progress?

master-mind picture master-mind · Jan 15, 2009 · Viewed 12.6k times · Source

I am working on a project that transfers files over a network and I want to incorporate a JProgressBar to show the progress during file transfer but I need help on that.

Answer

Nick Fortescue picture Nick Fortescue · Jan 16, 2009

You probably would find a ProgressMonitorInputStream easiest, but if that doesn't do enough for you, look at its source code to get exactly what you want.

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

To use a different transfer method then substitute an appropriate stream for FileInputStream.