0

According to the ScalaFX API, the dialog.onShown tasks run directly AFTER the dialog is shown.

I have some task that takes time. I need to open a "LOADING" dialog for the user to see while the task runs. This is an illustration of my code:

val dialog = new Dialog() {
  title = "LOADING"
  onShown = handle {
    myTask()
  }
}.showAndWait()

Of course my dialog has other content as well, which are irrelevant. The problem is when I run the program, the TASK is run first and then the dialog shows after it. I tried creating a new stage instead of dialog and faced the same problem. Tried inserting some println statements before the task to check, and indeed the printing was happening before the dialog shows. I also tried to let the program sleep for 5 seconds using Thread.sleep(5000) to give the dialog time to show (maybe it is freezing), with no luck at all.

H. Dayekh
  • 11
  • 1
  • Your method is blocking the UI thread, so the updates aren't shown until after it is complete. Use a [`Task`](https://docs.oracle.com/javase/8/javafx/api/javafx/concurrent/Task.html) or a `Thread`. See also: https://stackoverflow.com/questions/34985943/platform-runlater-issue-delay-execution – Itai Jul 04 '18 at 09:06
  • @Itai would you please elaborate as to where I should be using them? I am totally new to all of this, and thanks in advance – H. Dayekh Jul 04 '18 at 09:12
  • The [Oracle tutorial](https://docs.oracle.com/javase/8/javafx/interoperability-tutorial/concurrency.htm) is a good place to start. – Itai Jul 04 '18 at 09:13
  • Thanks, I'm seeing some results now. – H. Dayekh Jul 04 '18 at 09:28

0 Answers0