如何使用JProgressBar? [英] How to use JProgressBar?

查看:50
本文介绍了如何使用JProgressBar?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要从文本文件中加载一堆单词(大约70,000个),将其添加到哈希表中(使用soundex作为键)并对值进行排序.完成所有这些操作后,我想使用JProgressBar显示进度条.诸如

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 this and 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?

推荐答案

我将在专用工作线程而不是事件分发线程(EDT)上循环读取文本文件.如果我知道要读取的单词总数,那么我可以计算循环的每次迭代完成的百分比,并相应地更新进度条.

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.

示例代码

以下代码在预处理和后处理期间将进度条置于不确定模式,并显示指示工作正在进行的动画.从输入文件迭代读取时,使用确定模式.

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);
    }
});

建议

  • 考虑编写一种方便的方法来更新EDT上的进度条,以减少代码中的混乱情况( SwingUtilities.invokeLater ... public void run()... )

这篇关于如何使用JProgressBar?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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