JavaFX SwingWorker Equivalent?

后端 未结 3 1349
隐瞒了意图╮
隐瞒了意图╮ 2020-12-06 11:31

Is there a JavaFX equivalent to the Java SwingWorker class?

I am aware of the JavaFX Task but with that you can only publish String messages or a progress. I just wa

3条回答
  •  Happy的楠姐
    2020-12-06 11:56

    Apart from the updateMessage method where you can only pass strings, there is the updateValue method where you can pass a whole object, so I believe you can use that in a similar manner. This approach is described in the "A Task Which Returns Partial Results" section of the Task documentation. Another approach is the Platform.runLater() approach mentioned also in other answer.

    Note that an important difference between these approaches, is that the first one is coalescing the results, which means that for multiple frequent updateValue calls some may be omitted in order to protect flooding the FX thread.

    On the other hand, the Platform.runLater approach will send all the interim results, but due to the danger of flooding the FX thread if you have high frequency updates, some additional effort may be needed to manually avoid it like @eckig suggested in his answer which points to Throttling javafx gui updates

提交回复
热议问题