Norbert Preining
Norbert Preining

Reputation: 237

Scala ScalaFX: how to deal with large set of changes of Observable*

I am using a ObservableMap for data modeling, and want to update the whole entry. Initially the ObservableMap is empty, and it is filled asynchronously with lots of elements.

Now the problem is that an onChanged event is shot for each and every entry, which creates too many events and a bogging down of the GUI. I used pkgs.clear() ; pkgs ++= newpkgs.

Is there way to either only trigger one onChanged, either by disabling the handler temporarily, or by having an operation on the map that updates all elements but fires only afterwards.

Upvotes: 1

Views: 130

Answers (1)

Peter Lamberg
Peter Lamberg

Reputation: 8631

I'm not aware of a mechanism to disable/delay/buffer UI updates.

I don't know about other JavaFX view types, but I have experience with this using TableView in a similar situation. I suspect other views may be similar in this regard.

At least the TableView has a property called itemsProperty. When data in itemsProperty is updated using setItems (wrapper for itemsProperty.set(value)), the table tries very hard to update just the minimum slice of it self.

However, for the optimizations to work, the key is that the items must be "value objects" (hashCode and equals are deep and based on the actual data being displayed and not some random references.)

In the case of TableView this may require elaborate rowFactory and cellFactory implementations. The reason is that the data in items can't be "preformatted" in any way or it would spoil the optimizations inside TableView.

Realizing the above, solved my update churn problems. Maybe other answers can provide other tips.

Upvotes: 1

Related Questions