I have worked out how to do a progress bar. However, it does not function correctly. The progress bar is painted to a dialog box when a download is processed, but the progress bar will only show after the download is complete and shows 100%. Below is a snippect of my code, perhaps someone can tell me where I'm going wrong.
// A class for drawing the progress bar on a dialog box
class ProgressBar extends JDialog
{
private JProgressBar progress;
private JPanel panel;
public ProgressBar()
{
setTitle("Progress Monitor");
setSize(200, 100);
panel = new JPanel();
progress = new JProgressBar();
progress.setMinimum(0);
progress.setMaximum(100);
progress.setValue(0);
progress.setString(null);
progress.setStringPainted(true);
panel.add(progress);
add(panel);
}
// function to update the progress bar, called each time the percentage
// transfered is calculated
public void updatePB(int x)
{
progress.setValue(x);
}
}
// A class for monitoring bytes transfered and for calculating percentage
public class CustomFTPMonitor implements FTPProgressMonitor
{
static long filesize = 0;
private double percentage = 0;
public void bytesTransferred(long bytes)
{
if(percentage == 100)
{
percentage = 0;
}
// Calculate the percentagectransfered
percentage = ((double)bytes / (double)filesize)*100;
// pass the percentage to the publicly available update function
// for updating the progress bar
Didaftp.pb.updatePB((int)percentage);
}
}
// My main class
public static ProgressBar pb;
...
// Get tge size of the file
long fs = files[i].size();
// Create a new instance of my monitor
FTPProgressMonitor monitor = new CustomFTPMonitor();
// Create a new instance of ProgressBar and make is visible
pb = new ProgressBar();
pb.setVisible(true);
// pass the size of the file to the monitor
CustomFTPMonitor.filesize = fs;
ftp.setProgressMonitor(monitor,2048);
// download the file
ftp.get(path+"\\"+rFile,rFile);
...