Reflecting the updates to a ListCell that contains a mutable object

Hello

I have seen many variations of this question, but unfortunately have not found the answer, so I thought to ask here. My apologies if it is missing something obvious!

Objective:
I start individual tasks in batches. A ListCell reflects each task, from the initial request to the final result. Batches can be submitted even if a set is the treatment. When all any batch process is completed, they disappear finally in ListView.
Accordingly, I want a ListCell to reflect a task and reflect the transition from the initial design to the final outcome.

Location:

Currently, I am trying this with an ObservableList of my own POJO, each reflects by using a custom extension ListCell.

I got this result, but he was not good. First of all, I read that it is best practice not to change the objects of the ObservableList under his feet. However, I have multiple threads against the same list. With the objects being added, deleted, and updated, it seemed safer to update the object referenced rather than trying to manage the synchronization to avoid problems of simultaneous changes. After all, I'm not really add a new item, I'm wanting to upgrade the representation of an item which is now in a State of finished products.

Details of the attempt:

I realized that by building a "observableArrayList" with a reminder Extractor. This callback method offers an Observable range containing an ObjectProperty, the property being a member variable of my POJO used in each ListCell. Each task updates the property of this object with information at the end of his treatment. This I think ensure that change listeners are informed of updates to this POJO, via its ObjectProperty. (https://javafx-jira.kenai.com/browse/RT-15029)
The ListCell built with this POJO is tuned for updates, but I think it's really to reflect the additions and deletions on the ObservableList that represents the ListView. However, in the case of updates, the private enforcement of the updateItem ListCell (javafx.scene.control.ListCell #updateItem) method does not call the updateItem overridable method. This is because the object that caused the update is considered to be equal to the object the ListCell currently the wedges (they are the same instance, so it's true). However, if the updateItem overridable method is never called, I can't the ListCell customized to update its representation of the ListCell object has changed since his last performance was made.

If I do my fake return always custom of POJO in his overloaded is equivalent to the method, the overridable updateItem method is called, and I occasionally detects the change of the object and make a new representation. It works, but it feels wrong and as a hack.

Can anyone help direct me to the correct way to do this please? Happy to provide more information if necessary.

Thank you

Louis

If changes in the ObservableList must be reflected in the user interface, you must make these changes on the Thread of the JavaFX Application. To ensure that what is happening (if necessary using Platform.runLater (...)) will prevent also any question of simultaneous changes.

I would approach this by binding the text property or the graphics property (or two) the ListCell customized to suitable to your POJO properties. Here's a simple example where I have a list view of a bunch of "meters" that count in a background thread. The cells display is updated when the status of the task. I introduced a counter property (which is a little redundant; I could have used the property of progress or message) in order to demonstrate the update a property on the thread of the FX Application using Platform.runLater (...).

Because the ListCells can be reused, you must take into account that (task) is displayed may change during the life of the cell cycle. You can do this by using a listener to the itemProperty (which is what I did), or by substituting the updateItem (...) method.

Tags: Java

Similar Questions

Maybe you are looking for