Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ProgressBar doesn't change its value in Java

I have strange problem. I set a JProgressBar:

private JProgressBar progressBar;

public void foo()
{
    ...
    progressBar = new JProgressBar(0, 100);
    progressBar.setValue(0);
    progressBar.setStringPainted(true);
    ...
    contentPane.add(progressBar);
    ...
}

But it changes only when I put setValue function it in some places in code, not everywhere:

public void foo2()
{
    progressBar.setValue(100); //working
    if(...)
    {
        System.out.println("These instructions are executing"); //working
        progressBar.setValue(0);                                //not working
    }                             
}

So, what am I doing wrong? Why the second instruction doesn't work?

like image 474
Hoome Avatar asked Apr 17 '11 06:04

Hoome


People also ask

What is the Use of minimum and maximum value of ProgressBar in Java?

Set or get the minimum value of the progress monitor. This value is used by the monitor to set up the progress bar in the dialog. Set or get the maximum value of the progress monitor. This value is used by the monitor to set up the progress bar in the dialog.

How to create progress bar in Java Swing?

JProgressBar() : creates an progress bar with no text on it; JProgressBar(int orientation) : creates an progress bar with a specified orientation. if SwingConstants. VERTICAL is passed as argument a vertical progress bar is created, if SwingConstants.

What is the Use of minimum and maximum value of progress bar?

The Minimum and Maximum properties are used to set the minimum and maximum values that the progress bar can display. The Value property specifies the current position of the progress bar. The ProgressBar control is typically used when an application performs tasks such as copying files or printing documents.


1 Answers

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 that keep track of all the Swing components, and checks wether they have to be repainted. That is the thread that makes your JFrame come alive. When you hover a button and the color changes a bit, it's done by the AWTEventThread.

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

This means there are two solutions:

  1. (Recommend) You should create a separate 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()
        {
            public void run() {
            //Your original code with the loop here.
            }
        };
        Thread t = new Thread(runner, "Code Executer");
        t.start();
    }
    
  2. Manually repaint the progress bar. I did it always with bar.repaint(); but I'm wondering if it will work. I though it was that method. If that doesn't work, try: bar.update(bar.getGraphics());.

like image 102
Martijn Courteaux Avatar answered Oct 18 '22 08:10

Martijn Courteaux