A demonstration of the JProgressBar component /. Java Swing, 2nd Edition By Marc Loy, Robert Eckstein, Dave Wood, James Elliott, Brian Cole ISBN: 0-596-00408-7 Publisher: O'Reilly./ // SwingProgressBarExample.java // A demonstration of the JProgressBar component. The component tracks the // progress of a for loop. I'm extremely new to Java, and have mostly just been teaching myself as I go, so I've started building an applet. I'd like to make one that can select a file from the local disk and upload it as a multipart/form-data POST request but with a progress bar.Obviously the user has to grant permission to the Java applet to access the hard drive.
As useful as that is, there is still something else that we could add that would make copying a directory much more user friendly!Enter the progress bar from stage left. It's a useful utility that all big name software uses to tell the user something is happening and where it is with the task it is doing.So how do we incorporate this into copying a directory? Glad you asked, my question filled friend! The Progress BarFirst, we'll make a simple class to make printing progress 100 times easier as well as give us a nice reusable class to use in other projects that might need it.Here we go.
Java Progress Bar For File Copy
First we'll add the init method. EmptySymbol = emptySymbolThat wasn't too hard! We pass in the message we want to print, the width of the progress bar, and the two symbols for progress done and empty progress. We check that the width is always greater that or equal to zero, because we can't have a negative length! ÐŸ™‚Simple stuff.Alright, on to the hard part.
We have to figure out how to update and render the progress bar to the output window without printing a new line every single time.That's where the carriage return ( r) comes in. The carriage return is a special character that allows the printed message to start at the beginning of a line when printed. Every time we print something, we'll just use it at the beginning of our line and it should take care of our needs just fine. The only limitation this has is that if the progress bar happens to wrap in the terminal, the carriage return will not work as expected and will just output a new line.Here is our update function that will print the updated progress.
Flush ( )And, finally, we print the message to the screen. Why don't we just use the print function? Because the print function adds a new line to the end of our message and that's not what we want. We want it printed just as we set it.On with the show!
To the copying a directory function! Copying a Directory (Folder) with Progress in PythonIn order to know our progress, we'll have to sacrifice some speed. We need to count all of the files that we're copying to get the total amount of progress left, and this requires us to recursively search the directory and count all of the files.Let's write a function to do that.
Return len ( files )Pretty straight forward. We just checked if the directory passed in is a directory, then if it is, recursively walk the directories and add the files in the directories to the files list. Then we just return the length of the list.
Simple.Next is the copying directories function. If you looked at the article mentioned at the top of this article, you'll notice that I used the shutil.copytree function.
Here we can't do that because shutil doesn't support progress updates, so we'll have to write our own copying function.Let's do it!First we create an instance of our ProgressBar class. CalculateAndUpdate ( numCopied, numFiles )Here, we go through all the files, copy them, update the number of files copied so far, and then draw our progress bar.That's it!
Java Copy File Progress Bar Software
Now you have a nice copying function that will alert you of the progress while copying files. Moving a Directory (Folder) with Progress in PythonAs a note, you may also change the shutil.copy function call in copyFilesWithProgress to shutil.move and have a move progress bar instead.
All the other code would be the same, so I'm not going to rewrite it here. I'll leave that up to you! ÐŸ™‚Until the next time, I bid you adieu.