0

All the coding runs, my only problem is that the progress bar doesn't display the value changes. I set the values, but if I run it the value set, doesn't display on the progress bar. (The bar doesn't fill up)

public void logIn() {
    try {
        while (true) {
            calculate();
            System.out.println(loadingProgress);
            if (loadingProgress < 100) {
                mGUI.pbLoading.setValue(loadingProgress);
                Thread.sleep(random.nextInt(5000) + 100);
            } else {
                mGUI.pbLoading.setValue(100);
                break;
            }
        }
    } catch (Exception e) {
        e.printStackTrace();
    }
}

public void calculate() {
    random = new Random();
    loadingProgress = loadingProgress + random.nextInt(9) + 1;
}
skaffman
  • 398,947
  • 96
  • 818
  • 769
  • 1
    Seems here you block `EDT`. Read [Concurrency in Swing](https://docs.oracle.com/javase/tutorial/uiswing/concurrency/), also you need to use [`SwingWorker`](https://docs.oracle.com/javase/tutorial/uiswing/concurrency/simple.html) for long background task. Examine that [example](http://stackoverflow.com/a/15710912/2894369). – alex2410 May 05 '15 at 09:09
  • Exactly the [same situation I faced](http://stackoverflow.com/questions/29727991/jprogressbar-does-not-update-within-the-loop). [Here](http://stackoverflow.com/questions/29817219/changing-the-default-cursor-to-busy-cursor-does-not-work-as-expected) is the fixed code. Use a SwingWorker. – Spikatrix May 05 '15 at 11:22

3 Answers3

0

The calculation has to be done in an extra thread. This is because the currently running thread is blocking the repaint of the UI until the calculation is done.

derMrich
  • 316
  • 4
  • 12
  • 2
    No, not really, the progress bar needs to be updated on the EDT, otherwise you violate the single thread nature of the api. The work needs to be done on a separate thread, so as not to block the EDT – MadProgrammer May 05 '15 at 09:14
  • Yes, you are right, i confused both and will update my post. – derMrich May 05 '15 at 09:27
0

The problem is outside your posted code. I used your code to create a working example:

package snippet;

import java.util.Random;

public class Snippet {
    public class ValueAccepter {

        public void setValue(int loadingProgress) {
            System.out.println(loadingProgress);
        }

    }

    public class MGUI {

        public final ValueAccepter pbLoading = new ValueAccepter();

    }

    private int loadingProgress;
    private Random random;
    private MGUI mGUI = new MGUI();

    public static void main(String[] args) {
        new Snippet().logIn();
    }

    public void logIn() {
        try {
            while (true) {
                calculate();
                System.out.println(loadingProgress);
                if (loadingProgress < 100) {
                    mGUI.pbLoading.setValue(loadingProgress);
                    Thread.sleep(random.nextInt(5000) + 100);
                } else {
                    mGUI.pbLoading.setValue(100);
                    break;
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public void calculate() {
        random = new Random();
        loadingProgress = loadingProgress + random.nextInt(9) + 1;
    }
}

I think you have a problem with your swing-threads. Make sure, that mGUI.pbLoading.setValue is returning as expected.

slartidan
  • 20,403
  • 15
  • 83
  • 131
  • No, it's not. The login method is been executed within the context of the EDT, where as yours isn't, violating the single thread rules of Swing – MadProgrammer May 05 '15 at 09:19
  • @MadProgrammer You are right, if `pbLoading` is a Swing Component. In my example pbLoading is not a Swing Component - and everything works fine. If we had more code of Francois, we could tell whether the thread rules are violated. – slartidan May 05 '15 at 10:23
0

You have to use SwingWorker to execute graphical user interface operations and calculations otherwise the event dispatch thread won't be able to repaint. Here is a progress bar demo code by Oracle.

BullyWiiPlaza
  • 17,329
  • 10
  • 113
  • 185
  • You don't "have" to, but it is probably the preferred method. But if I were the op, I'd be asking why? Which you've not explained – MadProgrammer May 05 '15 at 09:21