1

I have a JavaFX UI that displays a relatively large data set. When the data set is changed, the changes are queued up and then the changes need to be "pushed" to the UI. Often there are more changes in the queue than I can push to the UI in one Platform.runLater() -- trying to push them all causes the JavaFX window to stutter or freeze.

Since the changes are in a queue, it's easy for me to break them into a series of batches. I would like to run a batch each time the javaFX ui thread loops.

How do I connect to the FX thread so it calls my method once every loop? In addition, about how many milliseconds can I responsibly use per-loop to update the data set?

Grumblesaurus
  • 3,021
  • 3
  • 31
  • 61
  • 3
    Use an [`AnimationTimer`](https://openjfx.io/javadoc/11/javafx.graphics/javafx/animation/AnimationTimer.html). – Slaw Mar 17 '19 at 10:44
  • 1
    Is that really the solution? The misnomer is bugging me out. – Grumblesaurus Mar 17 '19 at 16:09
  • 2
    In JavaFX, it's common and accepted practice to use the animation API to do short-lived things over time/periodically on the _JavaFX Application Thread_ (see [this question](https://stackoverflow.com/questions/9966136/javafx-periodic-background-task)). The animation API is really the only way to "loop" on the FX thread and `AnimationTimer` is basically the lowest level of that API. The `handle` method gets called once per frame (or "once every loop", to use your words). – Slaw Mar 17 '19 at 16:14
  • It would probably be easier to help if you posted a [mcve](https://stackoverflow.com/help/mcve), but typically long running processes would be done in a [Task](https://docs.oracle.com/javase/8/javafx/api/javafx/concurrent/Task.html) or a [Service](https://docs.oracle.com/javase/8/javafx/api/javafx/concurrent/Service.html). Using these classes allows you to get a result object, which could be a list. – RonSiven Mar 19 '19 at 00:36
  • 1
    @RonSiven The OP's problem doesn't seem to be related to loading the data (which is implied to happen on a background thread), but rather there's just too much data that when pushing it to the UI the UI stutters/freezes. Granted, that could be an issue with how the UI is designed, and a [mcve] would show that, but using a `Task` or `Service` wouldn't help. – Slaw Mar 19 '19 at 14:51

0 Answers0