0

I am new to Swing Applications.

I have a simple app that copies files from a directory to another one. To copy the files I use FileChannel instances. I was wondering if it is possible to show a progress bar to inform the user How much of the files have been copied.

Here is my code:

public class Foo extends JFrame{
    long size;
    FileChannel inp = null,
                outp= null;
    File sourceFile = new File("C:/test/movie.mkv"),
           destFile = new File("C:/output/movie.mkv");
    public void createUI(){
        JPanel panel = new JPanel();
        JButton btn = new JButton("Copy Files");
        panel.add(btn);
        /*code to create and position the jframe*/
        btn.addActionListener((e)->{
            try {
                inp = new FileInputStream(sourceFile).getChannel();
                outp = new FileOutputStream(destFile).getChannel();
                size = inp.size();
                outp.transferFrom(inp,0,size);
            } catch (Exception e2) {
                System.out.println("FIle not found");
                return;
            }
        });
    }
    public static void main(String[] args) {
        SwingUtilities.invokeLater(()->new Foo().createUI());
    }
}
John Kananakis
  • 133
  • 1
  • 9
  • See [`ProgressMonitorInputStream`](http://docs.oracle.com/javase/8/docs/api/javax/swing/ProgressMonitorInputStream.html) but note that the input stream will need to provide progress in a sensible form, and that it won't appear if the Event Dispatch Thread is blocked. Such things are best done using a `SwingWorker`, but I don't know how well that integrates with the PMIS. – Andrew Thompson Oct 01 '16 at 12:43
  • See http://stackoverflow.com/questions/26394714/how-to-use-progressmonitorinputstream – c0der Oct 01 '16 at 16:33

0 Answers0