JProgressBar Notes

Init

use setMinimum(0) and setMaximum(100);

Display with text

we can use setStringPainted(true) and then bar.setString to let the bar display custom text. But the content is limited inside the bar area.

If we need to display more text, we can use Textarea and append content to it.

Text area should be set to editable false.

We can clear the textarea by using setContent(“”);

bar not update

This may happen even if we use setValue(20) or sth.

The value of the progress bar is really updated. But it isn’t simply on the screen yet. Often, we use progress bars in loops. But, while you are in the loop, which you probably invoked by clicking a button it isn’t painted. Why? Because you invoked it by clicking a button. When you click a button, all the code you’ve made for that button is being executed by the AWTEventThread. This is the same thread which keep track of all Swing components which have to be repainted. That is the thread which make your JFrame come alive. When you hover a button and the color changes a bit, it’s the AWTEventThread.

So, while you are working in the loop, the AWTEventThread can’t update the screen anymore.

Solution: You should create a seperate thread which executes the loop. This means the AWTEventThread can update the screen if necessary (when you call bar.setValue(...);)

 

public void yourButtonClickMethod()
{
    Runnable runner = new Runnable()
    {
        // Your original code with the loop here.
    }
    Thread t = new Thread(runner, "Code Executer");
    t.start();
}
Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s