简体   繁体   English

如何在SftpProgressMonitor实例中使用现有的JProgressBar?

[英]How can I use existing JProgressBar in SftpProgressMonitor instance?

I am writing a program that downloads a file from a server using SFTP. 我正在编写一个程序,该程序使用SFTP从服务器下载文件。 I'd like to have a progress bar on the program that reflects the download progress. 我想在程序上有一个进度条,以反映下载进度。 Here is my current code for the progress monitor: 这是我当前的进度监视器代码:

public class SystemOutProgressMonitor implements SftpProgressMonitor
{
    public SystemOutProgressMonitor() {;}

    public void init(int op, java.lang.String src, java.lang.String dest, long max) 
    {
        System.out.println("STARTING: "+op+" "+src+" -> "+dest+" total: "+max);
    }

    public boolean count(long bytes)
    {
        for(int x=0;x<bytes;x++) {
            System.out.println("#");
        }
        return(true);
    }

    public void end()
    {
        System.out.println("\nFINISHED!");
    }
}

This works fine for outputting to the console, however, I do not know how to pass the existing progress bar variable to this. 这样可以很好地输出到控制台,但是,我不知道如何将现有的进度条变量传递给它。

The JProgressBar that I am using resides in my Menu.java class, in this class there is a download button that calls the corresponding download method in my Sftp.java class, from there, the SystemOutProgressMonitor is called by: 我正在使用的JProgressBar驻留在Menu.java类中,该类中有一个下载按钮,该调用按钮调用Sftp.java类中的相应下载方法,从那里,通过以下方式调用SystemOutProgressMonitor:

sftpChannel.get(target, destination, new SystemOutProgressMonitor());

So would I pass the progress bar from Menu.java to Sftp.java, and then to SystemOutProgressMonitor? 因此,我是否将进度条从Menu.java传递到Sftp.java,然后传递给SystemOutProgressMonitor?

Take a look at ProgressMonitor 看看ProgressMonitor

Otherwise you'll need a SwingWorker or use SwingUtilities.invokeLater to ensure that the updates to the JProgressBar are synchronized to the Event Dispatching Thread correctly. 否则,您将需要SwingWorker或使用SwingUtilities.invokeLater以确保对JProgressBar的更新已正确同步到事件调度线程。

Take a look at: 看一眼:

for more details 更多细节

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM