ssBar不会在setProgress上触发propertyCh

ssBar不会在setProgress上触发propertyCh

本文介绍了JProgressBar不会在setProgress上触发propertyChange的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经阅读了许多有关JProgressBar的文章……包括在Java上发现的狡猾代码.此处.

I've read many different articles about JProgressBar...including the dodgy code found over at Java; here.

多数表明您需要SwingWorker来使事情正确进行,这很合情理,我对此非常了解.我发现当我调用setProgress(value)来更新进度条时,大多数时候它不会触发propertyChange事件.我已经检查了传递给setProgess的值,并且每次都会更改它的值,因此我不确定它是否触发事件太快?请参见下面的相关代码,我们将不胜感激任何帮助/解释.

Most indicate you need a SwingWorker to get things happening properly, which makes perfect sense, I understand that much. I am finding that when I call setProgress(value) to update the progressbar, it's not triggering the propertyChange event most of the time. I've checked the value I'm passing to setProgess and it definitely changes every time, so I'm not sure if it's just firing the event too quickly? Please see relevant code below, any help/explanation would be greatly appreciated.

class ProgBar extends SwingWorker
{
    public ProgBar()
    {
        addPropertyChangeListener(new PropertyChangeListener()
        {
           @Override
           public void propertyChange(PropertyChangeEvent evt)
           {
               if ("progress".equals(evt.getPropertyName()))
               {
                   int value = (Integer)evt.getNewValue();
                   System.out.println("propertyChange called with: " + value);
                   loginProg.setValue(value);
               }
           }
        });

        loginProg.setStringPainted(true);
        loginProg.setValue(0);
        setProgress(0);
    }

    @Override
    public Void doInBackground() throws InterruptedException
    {
        ...
        int count = 0;
        for (Folder f : folders)
        {
            ... // process 'f'
            setProgress((int)Math.min(((double)count/folders.length)*100.0, 100.0));
        }
        ...
        return null;
    }

    @Override
    public void done()
    {
        System.out.println("Done called.");
        setProgress(100);
        loginProg.setValue(100);
    }
}

JProgressBar以此调用;

private void jButtonActionPerformed(java.awt.event.ActionEvent evt)
{
        // Create new thread to run progess bar.
        // Otherwise won't be able to update progress bar.
        ProgBar pb = new ProgBar();
        pb.execute();
    }
}


是的,所以我应该更好地阅读Javadocs;


Yeah, so I should have read the Javadocs better;

因为PropertyChangeListener是在事件调度线程上异步通知的,所以在调用任何PropertyChangeListener之前,可能会对setProgress方法进行多次调用.出于性能目的,所有这些调用仅与最后一个调用参数合并为一个调用.

例如,以下发票:
setProgress(1);
setProgress(2);
setProgress(3);

可能会导致单个PropertyChangeListener通知的值为3.

I.E.我认为setProgress触发得太快是正确的.ProgressMonitor可能是更好的解决方案.

I.E. my assumption that setProgress was firing too quickly was correct. A ProgressMonitor might be a better solution.

推荐答案

是的,所以我应该更好地阅读Javadocs;

Yeah, so I should have read the Javadocs better;

因为PropertyChangeListener是在事件调度线程上异步通知的,所以在调用任何PropertyChangeListener之前,可能会对setProgress方法进行多次调用.出于性能目的,所有这些调用仅与最后一个调用参数合并为一个调用.

例如,以下发票:
setProgress(1);
setProgress(2);
setProgress(3);
可能会导致单个PropertyChangeListener通知的值为3.

I.E.我认为setProgress触发得太快是正确的.ProgressMonitor可能是更好的解决方案.我已经通过SSCCE和我的程序确认了这一点,它们都只是太快地触发了setProgress,因此,只有传递给setProgress的最后一个值才传递给PropertyChange事件.

I.E. my assumption that setProgress was firing too quickly was correct. A ProgressMonitor might be a better solution. I've confirmed this with the SSCCE and my program, both are simply firing setProgress too quickly and as a result, only the last value passed to setProgress is being passed through to the PropertyChange event.

这篇关于JProgressBar不会在setProgress上触发propertyChange的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-21 10:21