0

I want to run a progressBar in a thread that shows the progress of a file loading procedure. The progress of the progressBar is dependent on the time of loading the file into a variable. The following code snippet shows the Thread.

    double progress=0;

    new Thread(){
            public void run() {
                    final double step = progress; //error: local variables referenced from an inner class must be final or effectively final
                    Platform.runLater(() -> pbs.setProgress(step));
            }
    }.start();

I'm aware of the impossibility of assigning the value of progress in an inner class but in any case I'm still trying to find a solution for forcing of getting values from a local variable. Are there any alternative solutions or approaches of getting values from a local variable?

Ramses
  • 652
  • 2
  • 8
  • 30
  • 2
    Can you explain what you're actually trying to do? Normally the progress would be updated as the `run()` method does its work, i.e. you would just declare it locally in the `run()` method. Why are you declaring it outside there? If you really need to access it somewhere else, then you're accessing it from multiple threads, so you either need to synchronize access, or use a `DoubleAdder` or `DoubleAccumulator`, depending on your actual use case. – James_D May 25 '16 at 23:01
  • @James_D All I want is to show the progress of a file-loading procedure, thats it. I noticed that the progressBar won't updates itself in a UI when when using *setProgress*. Therefore, I've put the progressBar into a separated thread but now I have difficulties in showing the right progress while my outer class is loading a file. How can I achieve a synchronization between my progressBar thread and my file loading method? Or are there any other solutions regarding to this? I'm newby in JavaFx. – Ramses May 26 '16 at 06:26
  • 1
    You don't need to put the progress bar in a separate thread. Load the file in the background thread and update the progress bar from a Platform.runLater() – James_D May 26 '16 at 10:36
  • I guess, to be clear: "I noticed that the progressBar won't update itself in a UI when using `setProgress`": it should, and if it doesn't, you're doing something else wrong. So maybe post that code and ask the question "Why isn't this working", instead of the code you posted, which doesn't really make sense (the thread you create isn't doing anything useful). – James_D May 26 '16 at 11:26
  • 1
    @James_D I found the solution under **http://stackoverflow.com/questions/26554814/javafx-updating-gui** thanks a lot! – Ramses Jun 01 '16 at 13:00

0 Answers0