0

I'm making a JavaFX application in which I read a problem and generate a solution when the start button is pressed. The problem is that when I click the start button, the GUI hangs. I looked at this post which suggests to wrap the thread-safe method in Platform.Runlater.

Now when the button is clicked, the method doBtnStartPressed() in my controller is called. This method only contains the call to the showSolution() method. Then I wrapped the content of this method in the Platform.runLater as follows:

private void showSolution() {
    Platform.runLater(new Runnable() {
        public void run() {
           // solve problem
           // draw solution on panes (rectangles which represent time windows)
        }
    });
}

This however doesn't work.

Community
  • 1
  • 1
thomasvdbossche
  • 107
  • 2
  • 9
  • 1
    The post you quote says (correctly) to "Use `PlatForm.RunLater` for quick and simple operations and `Task` for complex and big operations". You will need the concurrency framework (the `Task` and maybe more). See [here](http://docs.oracle.com/javase/8/javafx/interoperability-tutorial/concurrency.htm) for an introduction. – Nikos Paraskevopoulos Oct 22 '14 at 08:19
  • Thanks, I also tried to wrap the content of the showSolution() method in a Task after which I call new Thread(task).start(); This however doesn't throw any exceptions but nothing is drawn on the pane. I'm sure that the task is being started. – thomasvdbossche Oct 22 '14 at 08:32

2 Answers2

1

you can find a general overview of background tasks in JavaFX and how handle them here: http://www.guigarage.com/2014/10/datafx-8-released/ (scroll to the headline "ProgressChain").

In addition I mentioned this topic in my "JavaFX Enterprise" talk at JavaOne this year. You can find the slides here: http://de.slideshare.net/HendrikEbbers/javafx-enterprise-javaone-2014?related=1

A general discussion about Concurrency in JavaFX and UI Toolkits can be found here:

Hendrik Ebbers
  • 2,570
  • 19
  • 34
0

You need to solve the long running operation outside runLater and also construct the SceneGraph elements there (without actually attaching them to the parent) and then in one simply call add them to your real scene inside the runLater call.

tomsontom
  • 5,856
  • 2
  • 23
  • 21