-1

How would I have a Dialog that has an indeterminate ProgressIndicator in the foreground while the main thread loads a bunch of data in the background and eventually loads it on the main Scene. Currently, when the program loads, the page is is white, unresponsive, and just looks bad. Also, is this the best way to do this or is there a better way.

Kyefer
  • 98
  • 2
  • 12
  • There are many previous questions on concurrency in JavaFX as well as documentation at the Oracle JavaFX site, please search and research them. If you continue to have issues and require assistance post an [mcve](http://stackoverflow.com/help/mcve) that demonstrates your specific issue. – jewelsea Oct 15 '15 at 05:02

1 Answers1

0

Your window is frozen/unresponsive because you are loading this data in the main thread (which is the thread used by JavaFX to render your window).

What you need to do is perform the loading in a separate thread, which will run parallel to the main thread. When the loading is complete, this thread will notify the main thread and will pass the loaded data for you to render.

Here is quick way to create a new thread that loads your data:

            Thread myThread = new Thread(new Runnable() {
                @Override
                public void run() {

                    // Perform your loading here
                    // Once the loading is complete, notify the main thread

                }
            });
            myThread.start();

If you use this, your indeterminate ProgressIndicator will not be affected.

There are many ways to "notify the main thread". You could perhaps pass an instance of your main class to the secondary thread, and when the data is done loading you just call a method on that instance.

JVon
  • 684
  • 4
  • 10
  • I believe JavaFX will throw an error saying it's not on an FX thread – Kyefer Oct 15 '15 at 03:37
  • @Kyefer what exactly are you doing when "loading data"? JavaFX would say that if you are attempting to modify JavaFX elements in a different thread, but my understanding was that you were just loading data like reading large files or something. – JVon Oct 15 '15 at 03:48