Question

I'm hoping this is a simple question that doesn't require a SSCCE, since it would be very difficult for me to put one together that's short but includes all necessary parts.

I have a GUI (JFrame) that contains a JProgressBar. ProgressBar code inside createGUI():

progressBar = new JProgressBar(0, 100);
progressBar.setStringPainted(true);
progressBar.setBorder(BorderFactory.createEmptyBorder(5, 5, 5, 5));
progressBar.setValue(0);
progressBar.setIndeterminate(true);

The progress bar monitors the copying of files using the apache commons IO FileUtils/copyFile classes/methods. The string that is painted is the percentage (progress) and total MB copied so far. This all is done using a SwingWorker to keep the EDT clear of long-running processes. The progressBar is initially set to indeterminate, but after the second directory (to which the files will be copied) is created, the progress bar is set to regular (non-indeterminate) mode upon the first updateProgress() method call (updateProgress() is my own method in the SwingWorker class, which I named "task", created so that setProgress() can be accessed from outside the EDT). This is included inside class Task extends SwingWorker<Void, String>{}, along with the standard doInBackground() method, etc:

public void updateProgress(int tick){
    if(progressBar.isIndeterminate())
        progressBar.setIndeterminate(false);
    setProgress(tick);
}

I then created a PropertyChangeListener to update the progress bar whenever setProgress() is called with a new value (also inside createGUI()):

PropertyChangeListener listener = new PropertyChangeListener(){
    public void propertyChange(PropertyChangeEvent event){
        if("progress".equals(event.getPropertyName())){
            currentPercent = (int)event.getNewValue();
            progressBar.setValue((int)currentPercent);
            progressBar.setString(Integer.toString(newPercentValue) + "% (" + 
                                      String.format("%.2f", currentMB) + "MB of " + 
                                      String.format("%.2f", totalMB) + "MB)");
        }
    }
};

My problem is the following:

When the frame is first rendered, only the 0% appears and the "(xxMB of xxMB)" is not visible. The second part in parentheses only appears after the percentage changes for the first time. Is there a way I can update the MB values (displaying sizes of already-copied files and the total size) before the newPercentValue is changed? The way the listener currently sits, it (the percent AND the MB values) only update after the percentage increases for the first time. How might I update the MB values before the percentage changes from 0%?

I hope there is enough code here to go on. I would normally post a SSCCE but I honestly don't know how I'd do it in this case and include all the file size and percentage updates without adding in tons of code. I tried to include only the most important info, but feel free to request more if there's something else I should add. Many thanks!

Was it helpful?

Solution

Why not set the progress bar to 0% (and therefore fire a change event) when you find out the size of the file?

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top