Our Products:   CompleteFTP  edtFTPnet/Free  edtFTPnet/PRO  edtFTPj/Free  edtFTPj/PRO
0 votes
7.8k views
in Java FTP by (820 points)
I'm not really certain about how to do this as I'm fairly new to Java, but I wish to add a jprogress bar that shows the progress when an upload/download takes place. I've looked at some of the threads and example code in the test files. Could anyone provide some code samples on how to integrate them into an edtftpj app or perhaps a link to some beginners tutorials on JProgress bars.

Thanks in advance.

4 Answers

0 votes
by (820 points)
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);

...

0 votes
by (51.6k points)
Try executing the get() on a separate thread.

- Hans (EDT)
0 votes
by (820 points)
Thanks Hans, putting get() in a new thread worked. My Progress Bar is ticking along nicely now. Also it's stopped my program from hanging on large downloads.
0 votes
by (140 points)
Hi, I'm fairly new to Java and would like to implement progress monitor, but I don't know how to use threads can anyone help me with that, by giving an example of working progress monitor? I would be really grateful.

Categories

...