使用 SwingWorker 和 JProgressBar 跟踪多个图像下载的进度 [英] Tracking progress of multiple image downloads with SwingWorker and JProgressBar

查看:20
本文介绍了使用 SwingWorker 和 JProgressBar 跟踪多个图像下载的进度的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我很难找出使用 SwingWorker 跟踪多个图像下载的正确方法.我正在编写一个 GUI 程序,它将图像 URL 列表作为输入,读取每个图像,并使用 PDFBox 将所有图像保存为单个 PDF 文件.我有一个使用以下方法的下载器类:

I'm having a hard time figuring out the right way to use SwingWorker to track multiple image downloads. I'm writing a GUI program that takes a list of image URLs as input, reads each image, and uses PDFBox to save all the images as a single PDF file. I have a downloader class with the following method:

public void downloadImagesAsPDF(URL[] urlList, String filename){
   if(urlList.length == 0)
        return;

    //build pdf
    try {
        PDDocument doc = new PDDocument();

        for(URL imgLink : urllist){
            try{
                BufferedImage bi = ImageIO.read(imgLink);
                int width = bi.getWidth();
                int height = bi.getHeight();

                PDPage page = new PDPage(new PDRectangle(width, height));
                doc.addPage(page);

                PDXObjectImage image = new PDJpeg(doc, bi);

                PDPageContentStream content = new PDPageContentStream(doc, page);
                content.drawImage(image,0,0);
                content.close();
            }
            catch(Exception e){
                System.out.println(e.getMessage());
            }
        }
        doc.save(filename + ".pdf");
    }
    catch (Exception e) { 
        System.out.println(e.getMessage());
    }}

在另一个类中,我有一个带有 propertyChangeListener 的 SwingWorker 监听它并更新 JProgressBar(其余代码类似于教程中的 ProgressBarDemo,除了 doInBackground 方法):

On another class, I have a SwingWorker with a propertyChangeListener listening to it and updating a JProgressBar (rest of code is similar to ProgressBarDemo in the tutorials, except for the doInBackground method):

public Void doInBackground() {

    setProgress(0);
    URL[] URLList = {...}
    Downloader dl = new Downloader();
    dl.downloadImagesAsPDF(URLList, "some_images");  //need to track the progress inside this method.                   
    return null;
}

我希望能够在 downloadImagesAsPDF 方法中将 SwingWorker 的进度属性设置为(读取的字节数 * 100/图像的总大小).我能想到的唯一方法是将 SwingWorker 对象本身作为参数传递给重写的 downloadImagesAsPDF 方法,但这似乎是一个错误的设计决策.

I want to be able to set the progress property of SwingWorker within the downloadImagesAsPDF method as the (number of bytes read * 100 / total size of the images). The only way I can think of of doing this is to pass the SwingWorker object itself as a parameter to an overridden downloadImagesAsPDF method, but it seems like a wrong design decision.

另外,有没有办法找到使用下载方法中的 ImageIO.read(URL) 读取的字节数??提前致谢!

Also, is there a way to find the number of bytes read using the ImageIO.read(URL) inside the download method?? Thanks in advance!

推荐答案

SwingWorker 使您具有相当大的灵活性;没有必要让它支配你的设计.在这个示例中,松散耦合的PropertyChangeListener接收由工作人员调用setProgress().在这个示例中,每个worker都持有一个对组件JLabel的引用,用于直接记录进度,而主管线程等待 CountDownLatch.

You have considerable flexibility with SwingWorker; there's no need to let it dictate your design. In this example, a loosely coupled PropertyChangeListener receives notifications generated indirectly by the worker's call to setProgress(). In this example, each worker holds a reference to a component JLabel for recording progress directly, while a supervisor thread awaits a CountDownLatch.

这篇关于使用 SwingWorker 和 JProgressBar 跟踪多个图像下载的进度的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆