nus-cs2103-AY2223S2 / forum

12 stars 0 forks source link

Question about `ObservableList<T>` behaviour #294

Open jiasheng59 opened 1 year ago

jiasheng59 commented 1 year ago

Is it correct saying that ObservableList won't be able to detect changes in T itself? Instead the list will only get notified when there's change to the list itself (as compared to the change of internal attribute of T). If it's the case, what if we want to detect changes to the attribute of T, meanwhile also detect the change to the entire list, will the change to the internal attribute of T cascade to the ObservableList wrapping around it?

VietAnh1010 commented 1 year ago

You can try to:

The downside of this approach is that you have to know the index of the T you are going to update.

T oldT = list.get(index);
T newT = copy(oldT);
// modify newT
list.set(index, newT);

If you don't want to create new object, you may try:

T t = list.get(index);
// modify t
list.set(t);

Although I haven't tested this approach yet.

jiasheng59 commented 1 year ago

Yes, I think this could be the only approach. What I tried before is to make the attribute of T to be an ObservableValue / Property, e.g. SimpleIntegerProperty and see whether the change to the attribute will cascade to the list. But the GUI doesn't reflect the change automatically, but only changes when I explicitly click on the cell. (It's a bit strange)

Even when I use FilteredList, and set a trivial Predicate like p -> true to it, the GUI still doesn't reflect the change automatically.

So what I did in the end is to remove and add as what @VietAnh1010 suggested. Does anyone have any idea on this matter?