Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
400 views
in Technique[技术] by (71.8m points)

java - How to use 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?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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.

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()...)

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...