I would like to display a list of persons (coded in POJOS, and containing a name and surname property) using a JavaFX ListView control. I created the ListView and added the list of persons as an ObservableList. Everything works fine if I delete or add a new person to the ObservableList, but changes in the POJO do not trigger an update of the ListView. I have to remove and add the modified POJO from the ObservableList to trigger the update of the ListView. Is there any possibility to display changes in POJOS without the workaround described above?
相关问题
- Delete Messages from a Topic in Apache Kafka
- Jackson Deserialization not calling deserialize on
- How to maintain order of key-value in DataFrame sa
- StackExchange API - Deserialize Date in JSON Respo
- Difference between Types.INTEGER and Types.NULL in
You should take the observable list and update the object using list.set(selectedIndex, object); My example showing button with the handle method. In this I edited list users in fx viewtable
Using Francis idea I did:
May not be the best solution but worked.
There are several aspects to your question (and I'm not entirely which is the problem :-) I'll assume that your POJO somehow notifying listeners about changes, could be by being a full-fledged JavaBean, that is complies to its notification contract via firing propertyChange events as needed or some other means - otherwise, you would need some manual push of the change anyway.
The basic approach to make an FX-ObservableList notify its own listeners on mutations of contained elements is to configure it with a custom Callback that provides an array of Observables. If the elements have fx-properties you would do something like:
If the pojo is-a full-fledged core javaBean, its properties have to be adapted to fx-properties, f.i. by using JavaBeanProperty:
Note a caveat: without keeping a strong reference to the adapted properties somewhere, they will be quickly garbage-collected - and then appear to have no effect at all (falling into the trap again and again, not sure how if there's a good strategy to avoid it).
For any other means of (possibly coarse-grained) notification, you can implement a custom adapter: the adapter below listens to all propertyChanges of a bean, listening to other types of events would be quite analogous.
It's usage just the same as above (getting boring, isn't it :-)
Unfortunately, automatic updates of a ListView with such cool list won't work reliably due to a bug that's fixed only in jdk8. In earlier versions, you are back at square 1 - somehow listening to the change and then manually update the list:
You can manually trigger a
ListView.EditEvent
—which will cause theListView
to update—by calling theListView::fireEvent
method inherited fromjavafx.scene.Node
. For example,Or as a one liner,
Here is a sample application to demonstrate its use.
Since Java 8u60 ListView officially supports a method
refresh()
to update the view manually. JavaDoc:I successfully used this method for this issue here to update the content of items in the ListView.