简体   繁体   English

为什么第二个呼叫上没有显示ProgressMonitor?

[英]Why doesn't the ProgressMonitor display on second call?

I have written a program which uses the progressbar class from the default java runtime. 我编写了一个使用默认Java运行时中的progressbar类的程序。 It works perfectly except for the fact that it only works once. 它仅工作一次,但效果很好。 I use it to display the loading of various images into my GUI and it works fine on startup. 我使用它来显示将各种图像加载到我的GUI中,并且在启动时工作正常。 However, once I call it again it just displays a JFrame with no content. 但是,一旦我再次调用它,它只会显示一个没有内容的JFrame。 I can drag the JFrame around and everything, it's just that it won't display anything. 我可以拖动JFrame以及所有内容,只是它不会显示任何内容。 This is peculiar considering it works when it runs the first time, but not there after. 考虑到它在第一次运行时有效,但此后没有运行,这是很特殊的。 Here is my code: 这是我的代码:

    public void prefetch (){
    try{
        pageURLs = mangaEngine.getPageList();
        monitor = new ProgressMonitor(parent, "Loading Chapter..,", null, 0, mangaEngine.getPageList().length);
        pages = new StretchIconHQ[pageURLs.length];
        //Resets page counter to first page by fetching pages in reverse order
        monitor.setMaximum(pageURLs.length-1);
        for(int i = pageURLs.length-1; i>=0; i--){
            pages[i] = mangaEngine.loadImg(pageURLs[i]);
            monitor.setProgress(pageURLs.length-1-i);
            monitor.setNote("Loading Image:" + (pageURLs.length-1-i) + " of " + (pageURLs.length-1));
        }

    }
    catch(Exception ex){
        ex.printStackTrace();
    }
}

Any ideas on how to fix it? 关于如何解决它的任何想法?

You need to run the long-running code in a background thread such as that created by a SwingWorker. 您需要在诸如SwingWorker创建的后台线程中运行长时间运行的代码。

In fact, the Progress Monitor Tutorial describes how to do this exactly: 实际上,“ 进度监视器教程”描述了如何准确地做到这一点:

  • Create a SwingWorker 创建一个SwingWorker
  • Add a PropertyChangeListener to it 向其添加一个PropertyChangeListener
  • Listen to the "progress" property 听“ progress”属性
  • Update your monitor's progress with that of the SwingWorker's. 用SwingWorker的进度更新监视器的进度。

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

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