Java 如何使用 JProgressBar?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/277007/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to use JProgressBar?
提问by javac
I need to load a bunch of words (about 70,000) from a text file, add it to a hashtable (using soundex as a key) and sort the values. While doing all these I want to show a progress bar using JProgressBar. Articles such as thisand this, only gives a non-real example (a while loop). Can anyone suggest me how should I proceed. How can I get a number from above condition to set the value for the progress bar? Also it seems that there are different ways to do it - using thread, timer etc. Which could be the best method for the situation such as above?
我需要从文本文件中加载一堆单词(大约 70,000 个),将其添加到哈希表(使用 soundex 作为键)并对值进行排序。在执行所有这些操作时,我想使用 JProgressBar 显示一个进度条。诸如this和this 之类的文章,仅给出了一个非真实示例(while 循环)。任何人都可以建议我应该如何进行。如何从上述条件中获取一个数字来设置进度条的值?似乎也有不同的方法可以做到这一点 - 使用线程、计时器等。对于上述情况,哪种方法可能是最佳方法?
回答by Zach Scrivena
I would read the text file in a loop on a dedicated work thread, not the event-dispatch thread (EDT). If I know the total number of words to be read, then I can compute the percentage completed at each iteration of the loop and update the progress bar accordingly.
我会在专用工作线程上循环读取文本文件,而不是事件调度线程 (EDT)。如果我知道要阅读的总字数,那么我可以计算循环每次迭代完成的百分比并相应地更新进度条。
Sample Code
示例代码
The following code puts the progress bar in indeterminate mode during preprocessing and postprocessing, displaying an animation that indicates work is occurring. Determinate mode is used when reading iteratively from the input file.
以下代码在预处理和后处理期间将进度条置于不确定模式,显示一个指示工作正在进行的动画。从输入文件中迭代读取时使用确定模式。
// INITIALIZATION ON EDT
// JProgressBar progress = new JProgressBar();
// progress.setStringPainted(true);
// PREPROCESSING
// update progress bar (indeterminate mode)
SwingUtilities.invokeLater(new Runnable()
{
@Override
public void run()
{
progress.setIndeterminate(true);
progress.setString("Preprocessing...");
}
});
// perform preprocessing (open input file, determine total number of words, etc)
// PROCESSING
// update progress bar (switch to determinate mode)
SwingUtilities.invokeLater(new Runnable()
{
@Override
public void run()
{
progress.setIndeterminate(false);
}
});
int count = 0;
while (true)
{
// read a word from the input file; exit loop if EOF
// compute soundex representation
// add entry to map (hash table)
// compute percentage completed
count++;
final int percent = count * 100 / total;
// update progress bar on the EDT
SwingUtilities.invokeLater(new Runnable()
{
@Override
public void run()
{
progress.setString("Processing " + percent + "%");
progress.setValue(percent);
}
});
}
// POSTPROCESSING
// update progress bar (switch to indeterminate mode)
SwingUtilities.invokeLater(new Runnable()
{
@Override
public void run()
{
progress.setIndeterminate(true);
progress.setString("Postprocessing...");
}
});
// perform postprocessing (close input file, etc)
// DONE!
SwingUtilities.invokeLater(new Runnable()
{
@Override
public void run()
{
progress.setIndeterminate(false);
progress.setString("Done!");
progress.setValue(100);
}
});
Suggestions
建议
- Consider writing a convenience method to update the progress bar on the EDT, so as to reduce clutter in your code (
SwingUtilities.invokeLater... public void run()...
)
- 考虑编写一个方便的方法来更新 EDT 上的进度条,以减少代码中的混乱 (
SwingUtilities.invokeLater... public void run()...
)
回答by yanchenko
回答by Sean Anderson
Get the file size and count the bytes processed on each iteration. That way, you don't have to cycle through the file twice.
获取文件大小并计算每次迭代处理的字节数。这样,您就不必在文件中循环两次。